LCOV - code coverage report
Current view: top level - connectivity/source/commontools - conncleanup.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 52 67 77.6 %
Date: 2015-06-13 12:38:46 Functions: 8 11 72.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 <connectivity/conncleanup.hxx>
      21             : #include <com/sun/star/beans/XPropertySet.hpp>
      22             : #include <com/sun/star/lang/XComponent.hpp>
      23             : #include <osl/diagnose.h>
      24             : 
      25             : 
      26             : namespace dbtools
      27             : {
      28             : 
      29             : 
      30             :     using namespace ::com::sun::star::uno;
      31             :     using namespace ::com::sun::star::beans;
      32             :     using namespace ::com::sun::star::sdbc;
      33             :     using namespace ::com::sun::star::lang;
      34             : 
      35             :     static const char ACTIVE_CONNECTION_PROPERTY_NAME[] = "ActiveConnection";
      36             : 
      37          11 :     OAutoConnectionDisposer::OAutoConnectionDisposer(const Reference< XRowSet >& _rxRowSet, const Reference< XConnection >& _rxConnection)
      38             :         :m_xRowSet( _rxRowSet )
      39             :         ,m_bRSListening( false )
      40          11 :         ,m_bPropertyListening( false )
      41             :     {
      42          11 :         Reference< XPropertySet > xProps(_rxRowSet, UNO_QUERY);
      43             :         OSL_ENSURE(xProps.is(), "OAutoConnectionDisposer::OAutoConnectionDisposer: invalid rowset (no XPropertySet)!");
      44             : 
      45          11 :         if (!xProps.is())
      46          11 :             return;
      47             : 
      48             :         try
      49             :         {
      50          11 :             xProps->setPropertyValue( ACTIVE_CONNECTION_PROPERTY_NAME, makeAny( _rxConnection ) );
      51          11 :             m_xOriginalConnection = _rxConnection;
      52          11 :             startPropertyListening( xProps );
      53             :         }
      54           0 :         catch( const Exception& )
      55             :         {
      56             :             OSL_FAIL( "OAutoConnectionDisposer::OAutoConnectionDisposer: caught an exception!" );
      57          11 :         }
      58             :     }
      59             : 
      60             : 
      61          11 :     void OAutoConnectionDisposer::startPropertyListening( const Reference< XPropertySet >& _rxRowSet )
      62             :     {
      63             :         try
      64             :         {
      65          11 :             _rxRowSet->addPropertyChangeListener( ACTIVE_CONNECTION_PROPERTY_NAME, this );
      66          11 :             m_bPropertyListening = true;
      67             :         }
      68           0 :         catch( const Exception& )
      69             :         {
      70             :             OSL_FAIL( "OAutoConnectionDisposer::startPropertyListening: caught an exception!" );
      71             :         }
      72          11 :     }
      73             : 
      74             : 
      75          11 :     void OAutoConnectionDisposer::stopPropertyListening( const Reference< XPropertySet >& _rxEventSource )
      76             :     {
      77             :         // prevent deletion of ourself while we're herein
      78          11 :         Reference< XInterface > xKeepAlive(static_cast< XWeak* >(this));
      79             : 
      80             :         try
      81             :         {   // remove ourself as property change listener
      82             :             OSL_ENSURE( _rxEventSource.is(), "OAutoConnectionDisposer::stopPropertyListening: invalid event source (no XPropertySet)!" );
      83          11 :             if ( _rxEventSource.is() )
      84             :             {
      85          11 :                 _rxEventSource->removePropertyChangeListener( ACTIVE_CONNECTION_PROPERTY_NAME, this );
      86          11 :                 m_bPropertyListening = false;
      87             :             }
      88             :         }
      89           0 :         catch( const Exception& )
      90             :         {
      91             :             OSL_FAIL( "OAutoConnectionDisposer::stopPropertyListening: caught an exception!" );
      92          11 :         }
      93          11 :     }
      94             : 
      95             : 
      96          11 :     void OAutoConnectionDisposer::startRowSetListening()
      97             :     {
      98             :         OSL_ENSURE( !m_bRSListening, "OAutoConnectionDisposer::startRowSetListening: already listening!" );
      99             :         try
     100             :         {
     101          11 :             if ( !m_bRSListening )
     102          11 :                 m_xRowSet->addRowSetListener( this );
     103             :         }
     104           0 :         catch( const Exception& )
     105             :         {
     106             :             OSL_FAIL( "OAutoConnectionDisposer::startRowSetListening: caught an exception!" );
     107             :         }
     108          11 :         m_bRSListening = true;
     109          11 :     }
     110             : 
     111             : 
     112          11 :     void OAutoConnectionDisposer::stopRowSetListening()
     113             :     {
     114             :         OSL_ENSURE( m_bRSListening, "OAutoConnectionDisposer::stopRowSetListening: not listening!" );
     115             :         try
     116             :         {
     117          11 :             m_xRowSet->removeRowSetListener( this );
     118             :         }
     119           0 :         catch( const Exception& )
     120             :         {
     121             :             OSL_FAIL( "OAutoConnectionDisposer::stopRowSetListening: caught an exception!" );
     122             :         }
     123          11 :         m_bRSListening = false;
     124          11 :     }
     125             : 
     126             : 
     127          33 :     void SAL_CALL OAutoConnectionDisposer::propertyChange( const PropertyChangeEvent& _rEvent ) throw (RuntimeException, std::exception)
     128             :     {
     129          33 :         if ( _rEvent.PropertyName == ACTIVE_CONNECTION_PROPERTY_NAME )
     130             :         {   // somebody set a new ActiveConnection
     131             : 
     132          33 :             Reference< XConnection > xNewConnection;
     133          33 :             _rEvent.NewValue >>= xNewConnection;
     134             : 
     135          33 :             if ( isRowSetListening() )
     136             :             {
     137             :                 // we're listening at the row set, this means that the row set does not have our
     138             :                 // m_xOriginalConnection as active connection anymore
     139             :                 // So there are two possibilities
     140             :                 // a. somebody sets a new connection which is not our original one
     141             :                 // b. somebody sets a new connection, which is exactly the original one
     142             :                 // a. we're not interested in a, but in b: In this case, we simply need to move to the state
     143             :                 // we had originally: listen for property changes, do not listen for row set changes, and
     144             :                 // do not dispose the connection until the row set does not need it anymore
     145          22 :                 if ( xNewConnection.get() == m_xOriginalConnection.get() )
     146             :                 {
     147           0 :                     stopRowSetListening();
     148             :                 }
     149             :             }
     150             :             else
     151             :             {
     152             :                 // start listening at the row set. We're allowed to dispose the old connection as soon
     153             :                 // as the RowSet changed
     154             : 
     155             :                 // Unfortunately, the our database form implementations sometimes fire the change of their
     156             :                 // ActiveConnection twice. This is a error in forms/source/component/DatabaseForm.cxx, but
     157             :                 // changing this would require incompatible changes we can't do for a while.
     158             :                 // So for the moment, we have to live with it here.
     159             :                 //
     160             :                 // The only scenario where this doubled notification causes problems is when the connection
     161             :                 // of the form is reset to the one we're responsible for (m_xOriginalConnection), so we
     162             :                 // check this here.
     163             :                 //
     164             :                 // Yes, this is a HACK :(
     165          11 :                 if ( xNewConnection.get() != m_xOriginalConnection.get() )
     166             :                 {
     167             : #if OSL_DEBUG_LEVEL > 0
     168             :                     Reference< XConnection > xOldConnection;
     169             :                     _rEvent.OldValue >>= xOldConnection;
     170             :                     OSL_ENSURE( xOldConnection.get() == m_xOriginalConnection.get(), "OAutoConnectionDisposer::propertyChange: unexpected (original) property value!" );
     171             : #endif
     172          11 :                     startRowSetListening();
     173             :                 }
     174          33 :             }
     175             :         }
     176          33 :     }
     177             : 
     178             : 
     179          11 :     void SAL_CALL OAutoConnectionDisposer::disposing( const EventObject& _rSource ) throw (RuntimeException, std::exception)
     180             :     {
     181             :         // the rowset is being disposed, and nobody has set a new ActiveConnection in the meantime
     182          11 :         if ( isRowSetListening() )
     183          11 :             stopRowSetListening();
     184             : 
     185          11 :         clearConnection();
     186             : 
     187          11 :         if ( isPropertyListening() )
     188          11 :             stopPropertyListening( Reference< XPropertySet >( _rSource.Source, UNO_QUERY ) );
     189          11 :     }
     190             : 
     191          11 :     void OAutoConnectionDisposer::clearConnection()
     192             :     {
     193             :         try
     194             :         {
     195             :             // dispose the old connection
     196          11 :             Reference< XComponent > xComp(m_xOriginalConnection, UNO_QUERY);
     197          11 :             if (xComp.is())
     198          11 :                 xComp->dispose();
     199          11 :             m_xOriginalConnection.clear();
     200             :         }
     201           0 :         catch(Exception&)
     202             :         {
     203             :             OSL_FAIL("OAutoConnectionDisposer::clearConnection: caught an exception!");
     204             :         }
     205          11 :     }
     206             : 
     207           0 :     void SAL_CALL OAutoConnectionDisposer::cursorMoved( const ::com::sun::star::lang::EventObject& /*event*/ ) throw (::com::sun::star::uno::RuntimeException, std::exception)
     208             :     {
     209           0 :     }
     210             : 
     211           0 :     void SAL_CALL OAutoConnectionDisposer::rowChanged( const ::com::sun::star::lang::EventObject& /*event*/ ) throw (::com::sun::star::uno::RuntimeException, std::exception)
     212             :     {
     213           0 :     }
     214             : 
     215           0 :     void SAL_CALL OAutoConnectionDisposer::rowSetChanged( const ::com::sun::star::lang::EventObject& /*event*/ ) throw (::com::sun::star::uno::RuntimeException, std::exception)
     216             :     {
     217           0 :         stopRowSetListening();
     218           0 :         clearConnection();
     219             : 
     220           0 :     }
     221             : 
     222             : 
     223             : 
     224             : }   // namespace dbtools
     225             : 
     226             : 
     227             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11