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

Generated by: LCOV version 1.10