nss: upgrade to release 3.73
[LibreOffice.git] / connectivity / source / commontools / conncleanup.cxx
blobc33cc7289a6525c92596e3f4ca834181794ad174
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <connectivity/conncleanup.hxx>
21 #include <com/sun/star/beans/XPropertySet.hpp>
22 #include <com/sun/star/lang/XComponent.hpp>
23 #include <com/sun/star/sdbc/XRowSet.hpp>
24 #include <com/sun/star/sdbc/XConnection.hpp>
25 #include <osl/diagnose.h>
26 #include <tools/diagnose_ex.h>
29 namespace dbtools
33 using namespace css::uno;
34 using namespace css::beans;
35 using namespace css::sdbc;
36 using namespace css::lang;
38 const char ACTIVE_CONNECTION_PROPERTY_NAME[] = "ActiveConnection";
40 OAutoConnectionDisposer::OAutoConnectionDisposer(const Reference< XRowSet >& _rxRowSet, const Reference< XConnection >& _rxConnection)
41 :m_xRowSet( _rxRowSet )
42 ,m_bRSListening( false )
43 ,m_bPropertyListening( false )
45 Reference< XPropertySet > xProps(_rxRowSet, UNO_QUERY);
46 OSL_ENSURE(xProps.is(), "OAutoConnectionDisposer::OAutoConnectionDisposer: invalid rowset (no XPropertySet)!");
48 if (!xProps.is())
49 return;
51 try
53 xProps->setPropertyValue( ACTIVE_CONNECTION_PROPERTY_NAME, makeAny( _rxConnection ) );
54 m_xOriginalConnection = _rxConnection;
55 startPropertyListening( xProps );
57 catch( const Exception& )
59 TOOLS_WARN_EXCEPTION( "connectivity.commontools", "OAutoConnectionDisposer::OAutoConnectionDisposer" );
64 void OAutoConnectionDisposer::startPropertyListening( const Reference< XPropertySet >& _rxRowSet )
66 try
68 _rxRowSet->addPropertyChangeListener( ACTIVE_CONNECTION_PROPERTY_NAME, this );
69 m_bPropertyListening = true;
71 catch( const Exception& )
73 TOOLS_WARN_EXCEPTION( "connectivity.commontools", "OAutoConnectionDisposer::startPropertyListening" );
78 void OAutoConnectionDisposer::stopPropertyListening( const Reference< XPropertySet >& _rxEventSource )
80 // prevent deletion of ourself while we're herein
81 Reference< XInterface > xKeepAlive(static_cast< XWeak* >(this));
83 try
84 { // remove ourself as property change listener
85 OSL_ENSURE( _rxEventSource.is(), "OAutoConnectionDisposer::stopPropertyListening: invalid event source (no XPropertySet)!" );
86 if ( _rxEventSource.is() )
88 _rxEventSource->removePropertyChangeListener( ACTIVE_CONNECTION_PROPERTY_NAME, this );
89 m_bPropertyListening = false;
92 catch( const Exception& )
94 TOOLS_WARN_EXCEPTION( "connectivity.commontools", "OAutoConnectionDisposer::stopPropertyListening" );
99 void OAutoConnectionDisposer::startRowSetListening()
101 OSL_ENSURE( !m_bRSListening, "OAutoConnectionDisposer::startRowSetListening: already listening!" );
104 if ( !m_bRSListening )
105 m_xRowSet->addRowSetListener( this );
107 catch( const Exception& )
109 TOOLS_WARN_EXCEPTION( "connectivity.commontools", "OAutoConnectionDisposer::startRowSetListening" );
111 m_bRSListening = true;
115 void OAutoConnectionDisposer::stopRowSetListening()
117 OSL_ENSURE( m_bRSListening, "OAutoConnectionDisposer::stopRowSetListening: not listening!" );
120 m_xRowSet->removeRowSetListener( this );
122 catch( const Exception& )
124 TOOLS_WARN_EXCEPTION( "connectivity.commontools", "OAutoConnectionDisposer::stopRowSetListening" );
126 m_bRSListening = false;
130 void SAL_CALL OAutoConnectionDisposer::propertyChange( const PropertyChangeEvent& _rEvent )
132 if ( _rEvent.PropertyName != ACTIVE_CONNECTION_PROPERTY_NAME )
133 return;
135 // somebody set a new ActiveConnection
137 Reference< XConnection > xNewConnection;
138 _rEvent.NewValue >>= xNewConnection;
140 if ( isRowSetListening() )
142 // we're listening at the row set, this means that the row set does not have our
143 // m_xOriginalConnection as active connection anymore
144 // So there are two possibilities
145 // a. somebody sets a new connection which is not our original one
146 // b. somebody sets a new connection, which is exactly the original one
147 // a. we're not interested in a, but in b: In this case, we simply need to move to the state
148 // we had originally: listen for property changes, do not listen for row set changes, and
149 // do not dispose the connection until the row set does not need it anymore
150 if ( xNewConnection.get() == m_xOriginalConnection.get() )
152 stopRowSetListening();
155 else
157 // start listening at the row set. We're allowed to dispose the old connection as soon
158 // as the RowSet changed
160 // Unfortunately, the our database form implementations sometimes fire the change of their
161 // ActiveConnection twice. This is an error in forms/source/component/DatabaseForm.cxx, but
162 // changing this would require incompatible changes we can't do for a while.
163 // So for the moment, we have to live with it here.
165 // The only scenario where this doubled notification causes problems is when the connection
166 // of the form is reset to the one we're responsible for (m_xOriginalConnection), so we
167 // check this here.
169 // Yes, this is a HACK :(
170 if ( xNewConnection.get() != m_xOriginalConnection.get() )
172 #if OSL_DEBUG_LEVEL > 0
173 Reference< XConnection > xOldConnection;
174 _rEvent.OldValue >>= xOldConnection;
175 OSL_ENSURE( xOldConnection.get() == m_xOriginalConnection.get(), "OAutoConnectionDisposer::propertyChange: unexpected (original) property value!" );
176 #endif
177 startRowSetListening();
183 void SAL_CALL OAutoConnectionDisposer::disposing( const EventObject& _rSource )
185 // the rowset is being disposed, and nobody has set a new ActiveConnection in the meantime
186 if ( isRowSetListening() )
187 stopRowSetListening();
189 clearConnection();
191 if ( m_bPropertyListening )
192 stopPropertyListening( Reference< XPropertySet >( _rSource.Source, UNO_QUERY ) );
195 void OAutoConnectionDisposer::clearConnection()
199 // dispose the old connection
200 Reference< XComponent > xComp(m_xOriginalConnection, UNO_QUERY);
201 if (xComp.is())
202 xComp->dispose();
203 m_xOriginalConnection.clear();
205 catch(Exception&)
207 TOOLS_WARN_EXCEPTION("connectivity.commontools", "OAutoConnectionDisposer::clearConnection");
211 void SAL_CALL OAutoConnectionDisposer::cursorMoved( const css::lang::EventObject& /*event*/ )
215 void SAL_CALL OAutoConnectionDisposer::rowChanged( const css::lang::EventObject& /*event*/ )
219 void SAL_CALL OAutoConnectionDisposer::rowSetChanged( const css::lang::EventObject& /*event*/ )
221 stopRowSetListening();
222 clearConnection();
227 } // namespace dbtools
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */