1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
32 using namespace css::uno
;
33 using namespace css::beans
;
34 using namespace css::sdbc
;
35 using namespace css::lang
;
37 static const char ACTIVE_CONNECTION_PROPERTY_NAME
[] = "ActiveConnection";
39 OAutoConnectionDisposer::OAutoConnectionDisposer(const Reference
< XRowSet
>& _rxRowSet
, const Reference
< XConnection
>& _rxConnection
)
40 :m_xRowSet( _rxRowSet
)
41 ,m_bRSListening( false )
42 ,m_bPropertyListening( false )
44 Reference
< XPropertySet
> xProps(_rxRowSet
, UNO_QUERY
);
45 OSL_ENSURE(xProps
.is(), "OAutoConnectionDisposer::OAutoConnectionDisposer: invalid rowset (no XPropertySet)!");
52 xProps
->setPropertyValue( ACTIVE_CONNECTION_PROPERTY_NAME
, makeAny( _rxConnection
) );
53 m_xOriginalConnection
= _rxConnection
;
54 startPropertyListening( xProps
);
56 catch( const Exception
& )
58 OSL_FAIL( "OAutoConnectionDisposer::OAutoConnectionDisposer: caught an exception!" );
63 void OAutoConnectionDisposer::startPropertyListening( const Reference
< XPropertySet
>& _rxRowSet
)
67 _rxRowSet
->addPropertyChangeListener( ACTIVE_CONNECTION_PROPERTY_NAME
, this );
68 m_bPropertyListening
= true;
70 catch( const Exception
& )
72 OSL_FAIL( "OAutoConnectionDisposer::startPropertyListening: caught an exception!" );
77 void OAutoConnectionDisposer::stopPropertyListening( const Reference
< XPropertySet
>& _rxEventSource
)
79 // prevent deletion of ourself while we're herein
80 Reference
< XInterface
> xKeepAlive(static_cast< XWeak
* >(this));
83 { // remove ourself as property change listener
84 OSL_ENSURE( _rxEventSource
.is(), "OAutoConnectionDisposer::stopPropertyListening: invalid event source (no XPropertySet)!" );
85 if ( _rxEventSource
.is() )
87 _rxEventSource
->removePropertyChangeListener( ACTIVE_CONNECTION_PROPERTY_NAME
, this );
88 m_bPropertyListening
= false;
91 catch( const Exception
& )
93 OSL_FAIL( "OAutoConnectionDisposer::stopPropertyListening: caught an exception!" );
98 void OAutoConnectionDisposer::startRowSetListening()
100 OSL_ENSURE( !m_bRSListening
, "OAutoConnectionDisposer::startRowSetListening: already listening!" );
103 if ( !m_bRSListening
)
104 m_xRowSet
->addRowSetListener( this );
106 catch( const Exception
& )
108 OSL_FAIL( "OAutoConnectionDisposer::startRowSetListening: caught an exception!" );
110 m_bRSListening
= true;
114 void OAutoConnectionDisposer::stopRowSetListening()
116 OSL_ENSURE( m_bRSListening
, "OAutoConnectionDisposer::stopRowSetListening: not listening!" );
119 m_xRowSet
->removeRowSetListener( this );
121 catch( const Exception
& )
123 OSL_FAIL( "OAutoConnectionDisposer::stopRowSetListening: caught an exception!" );
125 m_bRSListening
= false;
129 void SAL_CALL
OAutoConnectionDisposer::propertyChange( const PropertyChangeEvent
& _rEvent
)
131 if ( _rEvent
.PropertyName
== ACTIVE_CONNECTION_PROPERTY_NAME
)
132 { // somebody set a new ActiveConnection
134 Reference
< XConnection
> xNewConnection
;
135 _rEvent
.NewValue
>>= xNewConnection
;
137 if ( isRowSetListening() )
139 // we're listening at the row set, this means that the row set does not have our
140 // m_xOriginalConnection as active connection anymore
141 // So there are two possibilities
142 // a. somebody sets a new connection which is not our original one
143 // b. somebody sets a new connection, which is exactly the original one
144 // a. we're not interested in a, but in b: In this case, we simply need to move to the state
145 // we had originally: listen for property changes, do not listen for row set changes, and
146 // do not dispose the connection until the row set does not need it anymore
147 if ( xNewConnection
.get() == m_xOriginalConnection
.get() )
149 stopRowSetListening();
154 // start listening at the row set. We're allowed to dispose the old connection as soon
155 // as the RowSet changed
157 // Unfortunately, the our database form implementations sometimes fire the change of their
158 // ActiveConnection twice. This is an error in forms/source/component/DatabaseForm.cxx, but
159 // changing this would require incompatible changes we can't do for a while.
160 // So for the moment, we have to live with it here.
162 // The only scenario where this doubled notification causes problems is when the connection
163 // of the form is reset to the one we're responsible for (m_xOriginalConnection), so we
166 // Yes, this is a HACK :(
167 if ( xNewConnection
.get() != m_xOriginalConnection
.get() )
169 #if OSL_DEBUG_LEVEL > 0
170 Reference
< XConnection
> xOldConnection
;
171 _rEvent
.OldValue
>>= xOldConnection
;
172 OSL_ENSURE( xOldConnection
.get() == m_xOriginalConnection
.get(), "OAutoConnectionDisposer::propertyChange: unexpected (original) property value!" );
174 startRowSetListening();
181 void SAL_CALL
OAutoConnectionDisposer::disposing( const EventObject
& _rSource
)
183 // the rowset is being disposed, and nobody has set a new ActiveConnection in the meantime
184 if ( isRowSetListening() )
185 stopRowSetListening();
189 if ( m_bPropertyListening
)
190 stopPropertyListening( Reference
< XPropertySet
>( _rSource
.Source
, UNO_QUERY
) );
193 void OAutoConnectionDisposer::clearConnection()
197 // dispose the old connection
198 Reference
< XComponent
> xComp(m_xOriginalConnection
, UNO_QUERY
);
201 m_xOriginalConnection
.clear();
205 OSL_FAIL("OAutoConnectionDisposer::clearConnection: caught an exception!");
209 void SAL_CALL
OAutoConnectionDisposer::cursorMoved( const css::lang::EventObject
& /*event*/ )
213 void SAL_CALL
OAutoConnectionDisposer::rowChanged( const css::lang::EventObject
& /*event*/ )
217 void SAL_CALL
OAutoConnectionDisposer::rowSetChanged( const css::lang::EventObject
& /*event*/ )
219 stopRowSetListening();
225 } // namespace dbtools
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */