LCOV - code coverage report
Current view: top level - forms/source/helper - windowstateguard.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 56 58 96.6 %
Date: 2014-04-11 Functions: 15 15 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "windowstateguard.hxx"
      21             : #include "frm_strings.hxx"
      22             : 
      23             : #include <com/sun/star/awt/XWindowListener2.hpp>
      24             : #include <com/sun/star/beans/XPropertySet.hpp>
      25             : #include <cppuhelper/implbase1.hxx>
      26             : #include <tools/diagnose_ex.h>
      27             : 
      28             : 
      29             : namespace frm
      30             : {
      31             : 
      32             : 
      33             :     using ::com::sun::star::awt::XWindowListener2;
      34             :     using ::com::sun::star::uno::Reference;
      35             :     using ::com::sun::star::awt::XWindow2;
      36             :     using ::com::sun::star::awt::WindowEvent;
      37             :     using ::com::sun::star::uno::RuntimeException;
      38             :     using ::com::sun::star::awt::XControlModel;
      39             :     using ::com::sun::star::beans::XPropertySet;
      40             :     using ::com::sun::star::lang::EventObject;
      41             :     using ::com::sun::star::uno::UNO_QUERY;
      42             :     using ::com::sun::star::uno::Exception;
      43             : 
      44             : 
      45             :     //= WindowStateGuard_Impl
      46             : 
      47             :     typedef ::cppu::WeakImplHelper1 <   XWindowListener2
      48             :                                     >   WindowStateGuard_Impl_Base;
      49         700 :     class WindowStateGuard_Impl : public WindowStateGuard_Impl_Base
      50             :     {
      51             :     private:
      52             :         ::osl::Mutex                m_aMutex;
      53             :         Reference< XWindow2 >       m_xWindow;
      54             :         Reference< XPropertySet >   m_xModelProps;
      55             : 
      56             :     public:
      57             :         /** constructs the instance
      58             :             @param _rxWindow
      59             :                 the window at which to listen. Must not be <NULL/>.
      60             :             @param _rxModel
      61             :                 the model which acts as the reference for the states to be enforced. Must not be <NULL/>.
      62             :         */
      63             :         WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps );
      64             : 
      65             :         void dispose();
      66             : 
      67             :     protected:
      68             :         // XWindowListener2
      69             :         virtual void SAL_CALL windowEnabled( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      70             :         virtual void SAL_CALL windowDisabled( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      71             : 
      72             :         // XWindowListener
      73             :         virtual void SAL_CALL windowResized( const ::com::sun::star::awt::WindowEvent& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      74             :         virtual void SAL_CALL windowMoved( const ::com::sun::star::awt::WindowEvent& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      75             :         virtual void SAL_CALL windowShown( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      76             :         virtual void SAL_CALL windowHidden( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      77             : 
      78             :         // XEventListener
      79             :         virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      80             : 
      81             :     private:
      82             :         /** ensures that the window's Enabled state matches what is described at the model
      83             :             @precond
      84             :                 our mutex is locked
      85             :         */
      86             :         void    impl_ensureEnabledState_nothrow_nolck();
      87             :     };
      88             : 
      89             : 
      90         353 :     WindowStateGuard_Impl::WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps )
      91             :         :m_xWindow( _rxWindow )
      92         353 :         ,m_xModelProps( _rxMdelProps )
      93             :     {
      94         353 :         if ( !m_xWindow.is() || !m_xModelProps.is() )
      95           0 :             throw RuntimeException();
      96             : 
      97         353 :         osl_atomic_increment( &m_refCount );
      98             :         {
      99         353 :             m_xWindow->addWindowListener( this );
     100             :         }
     101         353 :         osl_atomic_decrement( &m_refCount );
     102         353 :     }
     103             : 
     104             : 
     105         511 :     void WindowStateGuard_Impl::dispose()
     106             :     {
     107         511 :         ::osl::MutexGuard aGuard( m_aMutex );
     108         511 :         if ( !m_xWindow.is() )
     109             :             // already disposed
     110         669 :             return;
     111             : 
     112         353 :         m_xWindow->removeWindowListener( this );
     113         353 :         m_xWindow.clear();
     114             :     }
     115             : 
     116             : 
     117          24 :     void WindowStateGuard_Impl::impl_ensureEnabledState_nothrow_nolck()
     118             :     {
     119             :         try
     120             :         {
     121          24 :             Reference< XWindow2 > xWindow;
     122          48 :             Reference< XPropertySet > xModelProps;
     123          24 :             sal_Bool bShouldBeEnabled = sal_False;
     124             :             {
     125          24 :                 ::osl::MutexGuard aGuard( m_aMutex );
     126          24 :                 if ( !m_xWindow.is() || !m_xModelProps.is() )
     127          24 :                     return;
     128          24 :                 xWindow = m_xWindow;
     129          24 :                 xModelProps = m_xModelProps;
     130             :             }
     131             :             // fdo#42157: do not lock m_aMutex to prevent deadlock
     132          24 :             bool const bEnabled = xWindow->isEnabled();
     133          24 :             OSL_VERIFY( xModelProps->getPropertyValue( PROPERTY_ENABLED )
     134             :                         >>= bShouldBeEnabled );
     135             : 
     136          24 :             if ( !bShouldBeEnabled && bEnabled )
     137          33 :                 xWindow->setEnable( sal_False );
     138             :         }
     139           0 :         catch( const Exception& )
     140             :         {
     141             :             DBG_UNHANDLED_EXCEPTION();
     142             :         }
     143             :     }
     144             : 
     145             : 
     146          12 :     void SAL_CALL WindowStateGuard_Impl::windowEnabled( const EventObject& /*e*/ ) throw (RuntimeException, std::exception)
     147             :     {
     148          12 :         impl_ensureEnabledState_nothrow_nolck();
     149          12 :     }
     150             : 
     151             : 
     152          12 :     void SAL_CALL WindowStateGuard_Impl::windowDisabled( const EventObject& /*e*/ ) throw (RuntimeException, std::exception)
     153             :     {
     154          12 :         impl_ensureEnabledState_nothrow_nolck();
     155          12 :     }
     156             : 
     157             : 
     158         250 :     void SAL_CALL WindowStateGuard_Impl::windowResized( const WindowEvent& /*e*/ ) throw (RuntimeException, std::exception)
     159             :     {
     160             :         // not interested in
     161         250 :     }
     162             : 
     163             : 
     164         473 :     void SAL_CALL WindowStateGuard_Impl::windowMoved( const WindowEvent& /*e*/ ) throw (RuntimeException, std::exception)
     165             :     {
     166             :         // not interested in
     167         473 :     }
     168             : 
     169             : 
     170        2370 :     void SAL_CALL WindowStateGuard_Impl::windowShown( const EventObject& /*e*/ ) throw (RuntimeException, std::exception)
     171             :     {
     172             :         // not interested in
     173        2370 :     }
     174             : 
     175             : 
     176        2379 :     void SAL_CALL WindowStateGuard_Impl::windowHidden( const EventObject& /*e*/ ) throw (RuntimeException, std::exception)
     177             :     {
     178             :         // not interested in
     179        2379 :     }
     180             : 
     181             : 
     182         161 :     void SAL_CALL WindowStateGuard_Impl::disposing( const EventObject& Source ) throw (RuntimeException, std::exception)
     183             :     {
     184             :         OSL_ENSURE( Source.Source == m_xWindow, "WindowStateGuard_Impl::disposing: where does this come from?" );
     185             :         (void)Source;
     186         161 :         dispose();
     187         161 :     }
     188             : 
     189             : 
     190             :     //= WindowStateGuard
     191             : 
     192             : 
     193         319 :     WindowStateGuard::WindowStateGuard()
     194             :     {
     195         319 :     }
     196             : 
     197             : 
     198         275 :     WindowStateGuard::~WindowStateGuard()
     199             :     {
     200         275 :     }
     201             : 
     202             : 
     203         888 :     void WindowStateGuard::attach( const Reference< XWindow2 >& _rxWindow, const Reference< XControlModel >& _rxModel )
     204             :     {
     205         888 :         if ( m_pImpl.is() )
     206             :         {
     207         350 :             m_pImpl->dispose();
     208         350 :             m_pImpl = NULL;
     209             :         }
     210             : 
     211         888 :         Reference< XPropertySet > xModelProps( _rxModel, UNO_QUERY );
     212             :         OSL_ENSURE( xModelProps.is() || !_rxModel.is(), "WindowStateGuard::attach: a model which is no XPropertySet?" );
     213         888 :         if ( _rxWindow.is() && xModelProps.is() )
     214         353 :             m_pImpl = new WindowStateGuard_Impl( _rxWindow, xModelProps );
     215         888 :     }
     216             : 
     217             : 
     218             : } // namespace frm
     219             : 
     220             : 
     221             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10