LCOV - code coverage report
Current view: top level - forms/source/helper - windowstateguard.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 56 58 96.6 %
Date: 2015-06-13 12:38:46 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             :     typedef ::cppu::WeakImplHelper1 <   XWindowListener2
      45             :                                     >   WindowStateGuard_Impl_Base;
      46         728 :     class WindowStateGuard_Impl : public WindowStateGuard_Impl_Base
      47             :     {
      48             :     private:
      49             :         ::osl::Mutex                m_aMutex;
      50             :         Reference< XWindow2 >       m_xWindow;
      51             :         Reference< XPropertySet >   m_xModelProps;
      52             : 
      53             :     public:
      54             :         /** constructs the instance
      55             :             @param _rxWindow
      56             :                 the window at which to listen. Must not be <NULL/>.
      57             :             @param _rxModel
      58             :                 the model which acts as the reference for the states to be enforced. Must not be <NULL/>.
      59             :         */
      60             :         WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps );
      61             : 
      62             :         void dispose();
      63             : 
      64             :     protected:
      65             :         // XWindowListener2
      66             :         virtual void SAL_CALL windowEnabled( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      67             :         virtual void SAL_CALL windowDisabled( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      68             : 
      69             :         // XWindowListener
      70             :         virtual void SAL_CALL windowResized( const ::com::sun::star::awt::WindowEvent& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      71             :         virtual void SAL_CALL windowMoved( const ::com::sun::star::awt::WindowEvent& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      72             :         virtual void SAL_CALL windowShown( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      73             :         virtual void SAL_CALL windowHidden( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      74             : 
      75             :         // XEventListener
      76             :         virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      77             : 
      78             :     private:
      79             :         /** ensures that the window's Enabled state matches what is described at the model
      80             :             @precond
      81             :                 our mutex is locked
      82             :         */
      83             :         void    impl_ensureEnabledState_nothrow_nolck();
      84             :     };
      85             : 
      86             : 
      87         364 :     WindowStateGuard_Impl::WindowStateGuard_Impl( const Reference< XWindow2 >& _rxWindow, const Reference< XPropertySet >& _rxMdelProps )
      88             :         :m_xWindow( _rxWindow )
      89         364 :         ,m_xModelProps( _rxMdelProps )
      90             :     {
      91         364 :         if ( !m_xWindow.is() || !m_xModelProps.is() )
      92           0 :             throw RuntimeException();
      93             : 
      94         364 :         osl_atomic_increment( &m_refCount );
      95             :         {
      96         364 :             m_xWindow->addWindowListener( this );
      97             :         }
      98         364 :         osl_atomic_decrement( &m_refCount );
      99         364 :     }
     100             : 
     101             : 
     102         522 :     void WindowStateGuard_Impl::dispose()
     103             :     {
     104         522 :         ::osl::MutexGuard aGuard( m_aMutex );
     105         522 :         if ( !m_xWindow.is() )
     106             :             // already disposed
     107         680 :             return;
     108             : 
     109         364 :         m_xWindow->removeWindowListener( this );
     110         364 :         m_xWindow.clear();
     111             :     }
     112             : 
     113             : 
     114          24 :     void WindowStateGuard_Impl::impl_ensureEnabledState_nothrow_nolck()
     115             :     {
     116             :         try
     117             :         {
     118          24 :             Reference< XWindow2 > xWindow;
     119          48 :             Reference< XPropertySet > xModelProps;
     120          24 :             bool bShouldBeEnabled = false;
     121             :             {
     122          24 :                 ::osl::MutexGuard aGuard( m_aMutex );
     123          24 :                 if ( !m_xWindow.is() || !m_xModelProps.is() )
     124          24 :                     return;
     125          24 :                 xWindow = m_xWindow;
     126          24 :                 xModelProps = m_xModelProps;
     127             :             }
     128             :             // fdo#42157: do not lock m_aMutex to prevent deadlock
     129          24 :             bool const bEnabled = xWindow->isEnabled();
     130          24 :             OSL_VERIFY( xModelProps->getPropertyValue( PROPERTY_ENABLED )
     131             :                         >>= bShouldBeEnabled );
     132             : 
     133          24 :             if ( !bShouldBeEnabled && bEnabled )
     134          33 :                 xWindow->setEnable( sal_False );
     135             :         }
     136           0 :         catch( const Exception& )
     137             :         {
     138             :             DBG_UNHANDLED_EXCEPTION();
     139             :         }
     140             :     }
     141             : 
     142             : 
     143          12 :     void SAL_CALL WindowStateGuard_Impl::windowEnabled( const EventObject& /*e*/ ) throw (RuntimeException, std::exception)
     144             :     {
     145          12 :         impl_ensureEnabledState_nothrow_nolck();
     146          12 :     }
     147             : 
     148             : 
     149          12 :     void SAL_CALL WindowStateGuard_Impl::windowDisabled( const EventObject& /*e*/ ) throw (RuntimeException, std::exception)
     150             :     {
     151          12 :         impl_ensureEnabledState_nothrow_nolck();
     152          12 :     }
     153             : 
     154             : 
     155         251 :     void SAL_CALL WindowStateGuard_Impl::windowResized( const WindowEvent& /*e*/ ) throw (RuntimeException, std::exception)
     156             :     {
     157             :         // not interested in
     158         251 :     }
     159             : 
     160             : 
     161         496 :     void SAL_CALL WindowStateGuard_Impl::windowMoved( const WindowEvent& /*e*/ ) throw (RuntimeException, std::exception)
     162             :     {
     163             :         // not interested in
     164         496 :     }
     165             : 
     166             : 
     167        3242 :     void SAL_CALL WindowStateGuard_Impl::windowShown( const EventObject& /*e*/ ) throw (RuntimeException, std::exception)
     168             :     {
     169             :         // not interested in
     170        3242 :     }
     171             : 
     172             : 
     173        3266 :     void SAL_CALL WindowStateGuard_Impl::windowHidden( const EventObject& /*e*/ ) throw (RuntimeException, std::exception)
     174             :     {
     175             :         // not interested in
     176        3266 :     }
     177             : 
     178             : 
     179         158 :     void SAL_CALL WindowStateGuard_Impl::disposing( const EventObject& Source ) throw (RuntimeException, std::exception)
     180             :     {
     181             :         OSL_ENSURE( Source.Source == m_xWindow, "WindowStateGuard_Impl::disposing: where does this come from?" );
     182             :         (void)Source;
     183         158 :         dispose();
     184         158 :     }
     185             : 
     186         341 :     WindowStateGuard::WindowStateGuard()
     187             :     {
     188         341 :     }
     189             : 
     190             : 
     191         302 :     WindowStateGuard::~WindowStateGuard()
     192             :     {
     193         302 :     }
     194             : 
     195             : 
     196         934 :     void WindowStateGuard::attach( const Reference< XWindow2 >& _rxWindow, const Reference< XControlModel >& _rxModel )
     197             :     {
     198         934 :         if ( m_pImpl.is() )
     199             :         {
     200         364 :             m_pImpl->dispose();
     201         364 :             m_pImpl = NULL;
     202             :         }
     203             : 
     204         934 :         Reference< XPropertySet > xModelProps( _rxModel, UNO_QUERY );
     205             :         OSL_ENSURE( xModelProps.is() || !_rxModel.is(), "WindowStateGuard::attach: a model which is no XPropertySet?" );
     206         934 :         if ( _rxWindow.is() && xModelProps.is() )
     207         364 :             m_pImpl = new WindowStateGuard_Impl( _rxWindow, xModelProps );
     208         934 :     }
     209             : 
     210             : 
     211             : } // namespace frm
     212             : 
     213             : 
     214             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11