LCOV - code coverage report
Current view: top level - connectivity/source/commontools - conncleanup.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 55 70 78.6 %
Date: 2012-08-25 Functions: 9 12 75.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 48 112 42.9 %

           Branch data     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                 :        132 :     static const ::rtl::OUString& getActiveConnectionPropertyName()
      37                 :            :     {
      38 [ +  + ][ +  - ]:        132 :         static const ::rtl::OUString s_sActiveConnectionPropertyName( RTL_CONSTASCII_USTRINGPARAM( "ActiveConnection" ));
         [ +  - ][ #  # ]
      39                 :        132 :         return s_sActiveConnectionPropertyName;
      40                 :            :     }
      41                 :            : 
      42                 :            :     //=====================================================================
      43                 :            :     //= OAutoConnectionDisposer
      44                 :            :     //=====================================================================
      45                 :            :     //---------------------------------------------------------------------
      46                 :         22 :     OAutoConnectionDisposer::OAutoConnectionDisposer(const Reference< XRowSet >& _rxRowSet, const Reference< XConnection >& _rxConnection)
      47                 :            :         :m_xRowSet( _rxRowSet )
      48                 :            :         ,m_bRSListening( sal_False )
      49                 :         22 :         ,m_bPropertyListening( sal_False )
      50                 :            :     {
      51         [ +  - ]:         22 :         Reference< XPropertySet > xProps(_rxRowSet, UNO_QUERY);
      52                 :            :         OSL_ENSURE(xProps.is(), "OAutoConnectionDisposer::OAutoConnectionDisposer: invalid rowset (no XPropertySet)!");
      53                 :            : 
      54         [ -  + ]:         22 :         if (!xProps.is())
      55                 :         22 :             return;
      56                 :            : 
      57                 :            :         try
      58                 :            :         {
      59 [ +  - ][ +  - ]:         22 :             xProps->setPropertyValue( getActiveConnectionPropertyName(), makeAny( _rxConnection ) );
         [ +  - ][ +  - ]
                 [ #  # ]
      60         [ +  - ]:         22 :             m_xOriginalConnection = _rxConnection;
      61         [ +  - ]:         22 :             startPropertyListening( xProps );
      62                 :            :         }
      63         [ #  # ]:          0 :         catch( const Exception& )
      64                 :            :         {
      65                 :            :             OSL_FAIL( "OAutoConnectionDisposer::OAutoConnectionDisposer: caught an exception!" );
      66         [ +  - ]:         22 :         }
      67                 :            :     }
      68                 :            : 
      69                 :            :     //---------------------------------------------------------------------
      70                 :         22 :     void OAutoConnectionDisposer::startPropertyListening( const Reference< XPropertySet >& _rxRowSet )
      71                 :            :     {
      72                 :            :         try
      73                 :            :         {
      74 [ +  - ][ +  - ]:         22 :             _rxRowSet->addPropertyChangeListener( getActiveConnectionPropertyName(), this );
         [ +  - ][ +  - ]
                 [ #  # ]
      75                 :         22 :             m_bPropertyListening = sal_True;
      76                 :            :         }
      77                 :          0 :         catch( const Exception& )
      78                 :            :         {
      79                 :            :             OSL_FAIL( "OAutoConnectionDisposer::startPropertyListening: caught an exception!" );
      80                 :            :         }
      81                 :         22 :     }
      82                 :            : 
      83                 :            :     //---------------------------------------------------------------------
      84                 :         22 :     void OAutoConnectionDisposer::stopPropertyListening( const Reference< XPropertySet >& _rxEventSource )
      85                 :            :     {
      86                 :            :         // prevent deletion of ourself while we're herein
      87         [ +  - ]:         22 :         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         [ +  - ]:         22 :             if ( _rxEventSource.is() )
      93                 :            :             {
      94 [ +  - ][ +  - ]:         22 :                 _rxEventSource->removePropertyChangeListener( getActiveConnectionPropertyName(), this );
         [ +  - ][ +  - ]
                 [ #  # ]
      95                 :         22 :                 m_bPropertyListening = sal_False;
      96                 :            :             }
      97                 :            :         }
      98         [ #  # ]:          0 :         catch( const Exception& )
      99                 :            :         {
     100                 :            :             OSL_FAIL( "OAutoConnectionDisposer::stopPropertyListening: caught an exception!" );
     101                 :         22 :         }
     102                 :         22 :     }
     103                 :            : 
     104                 :            :     //---------------------------------------------------------------------
     105                 :         22 :     void OAutoConnectionDisposer::startRowSetListening()
     106                 :            :     {
     107                 :            :         OSL_ENSURE( !m_bRSListening, "OAutoConnectionDisposer::startRowSetListening: already listening!" );
     108                 :            :         try
     109                 :            :         {
     110         [ +  - ]:         22 :             if ( !m_bRSListening )
     111 [ +  - ][ +  - ]:         22 :                 m_xRowSet->addRowSetListener( this );
         [ +  - ][ #  # ]
     112                 :            :         }
     113                 :          0 :         catch( const Exception& )
     114                 :            :         {
     115                 :            :             OSL_FAIL( "OAutoConnectionDisposer::startRowSetListening: caught an exception!" );
     116                 :            :         }
     117                 :         22 :         m_bRSListening = sal_True;
     118                 :         22 :     }
     119                 :            : 
     120                 :            :     //---------------------------------------------------------------------
     121                 :         22 :     void OAutoConnectionDisposer::stopRowSetListening()
     122                 :            :     {
     123                 :            :         OSL_ENSURE( m_bRSListening, "OAutoConnectionDisposer::stopRowSetListening: not listening!" );
     124                 :            :         try
     125                 :            :         {
     126 [ +  - ][ +  - ]:         22 :             m_xRowSet->removeRowSetListener( this );
         [ +  - ][ #  # ]
     127                 :            :         }
     128                 :          0 :         catch( const Exception& )
     129                 :            :         {
     130                 :            :             OSL_FAIL( "OAutoConnectionDisposer::stopRowSetListening: caught an exception!" );
     131                 :            :         }
     132                 :         22 :         m_bRSListening = sal_False;
     133                 :         22 :     }
     134                 :            : 
     135                 :            :     //---------------------------------------------------------------------
     136                 :         66 :     void SAL_CALL OAutoConnectionDisposer::propertyChange( const PropertyChangeEvent& _rEvent ) throw (RuntimeException)
     137                 :            :     {
     138         [ +  - ]:         66 :         if ( _rEvent.PropertyName.equals( getActiveConnectionPropertyName() ) )
     139                 :            :         {   // somebody set a new ActiveConnection
     140                 :            : 
     141                 :         66 :             Reference< XConnection > xNewConnection;
     142         [ +  - ]:         66 :             _rEvent.NewValue >>= xNewConnection;
     143                 :            : 
     144         [ +  + ]:         66 :             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 [ +  - ][ +  - ]:         44 :                 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 [ +  - ][ +  - ]:         22 :                 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         [ +  - ]:         22 :                     startRowSetListening();
     182                 :            :                 }
     183                 :         66 :             }
     184                 :            :         }
     185                 :         66 :     }
     186                 :            : 
     187                 :            :     //---------------------------------------------------------------------
     188                 :         22 :     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         [ +  - ]:         22 :         if ( isRowSetListening() )
     192                 :         22 :             stopRowSetListening();
     193                 :            : 
     194                 :         22 :         clearConnection();
     195                 :            : 
     196         [ +  - ]:         22 :         if ( isPropertyListening() )
     197         [ +  - ]:         22 :             stopPropertyListening( Reference< XPropertySet >( _rSource.Source, UNO_QUERY ) );
     198                 :         22 :     }
     199                 :            :     //---------------------------------------------------------------------
     200                 :         22 :     void OAutoConnectionDisposer::clearConnection()
     201                 :            :     {
     202                 :            :         try
     203                 :            :         {
     204                 :            :             // dispose the old connection
     205         [ +  - ]:         22 :             Reference< XComponent > xComp(m_xOriginalConnection, UNO_QUERY);
     206         [ +  - ]:         22 :             if (xComp.is())
     207 [ +  - ][ +  - ]:         22 :                 xComp->dispose();
     208         [ #  # ]:         22 :             m_xOriginalConnection.clear();
     209                 :            :         }
     210                 :          0 :         catch(Exception&)
     211                 :            :         {
     212                 :            :             OSL_FAIL("OAutoConnectionDisposer::clearConnection: caught an exception!");
     213                 :            :         }
     214                 :         22 :     }
     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