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 <osl/diagnose.h>
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
;
35 static const char ACTIVE_CONNECTION_PROPERTY_NAME
[] = "ActiveConnection";
37 OAutoConnectionDisposer::OAutoConnectionDisposer(const Reference
< XRowSet
>& _rxRowSet
, const Reference
< XConnection
>& _rxConnection
)
38 :m_xRowSet( _rxRowSet
)
39 ,m_bRSListening( false )
40 ,m_bPropertyListening( false )
42 Reference
< XPropertySet
> xProps(_rxRowSet
, UNO_QUERY
);
43 OSL_ENSURE(xProps
.is(), "OAutoConnectionDisposer::OAutoConnectionDisposer: invalid rowset (no XPropertySet)!");
50 xProps
->setPropertyValue( ACTIVE_CONNECTION_PROPERTY_NAME
, makeAny( _rxConnection
) );
51 m_xOriginalConnection
= _rxConnection
;
52 startPropertyListening( xProps
);
54 catch( const Exception
& )
56 OSL_FAIL( "OAutoConnectionDisposer::OAutoConnectionDisposer: caught an exception!" );
61 void OAutoConnectionDisposer::startPropertyListening( const Reference
< XPropertySet
>& _rxRowSet
)
65 _rxRowSet
->addPropertyChangeListener( ACTIVE_CONNECTION_PROPERTY_NAME
, this );
66 m_bPropertyListening
= true;
68 catch( const Exception
& )
70 OSL_FAIL( "OAutoConnectionDisposer::startPropertyListening: caught an exception!" );
75 void OAutoConnectionDisposer::stopPropertyListening( const Reference
< XPropertySet
>& _rxEventSource
)
77 // prevent deletion of ourself while we're herein
78 Reference
< XInterface
> xKeepAlive(static_cast< XWeak
* >(this));
81 { // remove ourself as property change listener
82 OSL_ENSURE( _rxEventSource
.is(), "OAutoConnectionDisposer::stopPropertyListening: invalid event source (no XPropertySet)!" );
83 if ( _rxEventSource
.is() )
85 _rxEventSource
->removePropertyChangeListener( ACTIVE_CONNECTION_PROPERTY_NAME
, this );
86 m_bPropertyListening
= false;
89 catch( const Exception
& )
91 OSL_FAIL( "OAutoConnectionDisposer::stopPropertyListening: caught an exception!" );
96 void OAutoConnectionDisposer::startRowSetListening()
98 OSL_ENSURE( !m_bRSListening
, "OAutoConnectionDisposer::startRowSetListening: already listening!" );
101 if ( !m_bRSListening
)
102 m_xRowSet
->addRowSetListener( this );
104 catch( const Exception
& )
106 OSL_FAIL( "OAutoConnectionDisposer::startRowSetListening: caught an exception!" );
108 m_bRSListening
= true;
112 void OAutoConnectionDisposer::stopRowSetListening()
114 OSL_ENSURE( m_bRSListening
, "OAutoConnectionDisposer::stopRowSetListening: not listening!" );
117 m_xRowSet
->removeRowSetListener( this );
119 catch( const Exception
& )
121 OSL_FAIL( "OAutoConnectionDisposer::stopRowSetListening: caught an exception!" );
123 m_bRSListening
= false;
127 void SAL_CALL
OAutoConnectionDisposer::propertyChange( const PropertyChangeEvent
& _rEvent
) throw (RuntimeException
, std::exception
)
129 if ( _rEvent
.PropertyName
== ACTIVE_CONNECTION_PROPERTY_NAME
)
130 { // somebody set a new ActiveConnection
132 Reference
< XConnection
> xNewConnection
;
133 _rEvent
.NewValue
>>= xNewConnection
;
135 if ( isRowSetListening() )
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 if ( xNewConnection
.get() == m_xOriginalConnection
.get() )
147 stopRowSetListening();
152 // start listening at the row set. We're allowed to dispose the old connection as soon
153 // as the RowSet changed
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.
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
164 // Yes, this is a HACK :(
165 if ( xNewConnection
.get() != m_xOriginalConnection
.get() )
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!" );
172 startRowSetListening();
179 void SAL_CALL
OAutoConnectionDisposer::disposing( const EventObject
& _rSource
) throw (RuntimeException
, std::exception
)
181 // the rowset is being disposed, and nobody has set a new ActiveConnection in the meantime
182 if ( isRowSetListening() )
183 stopRowSetListening();
187 if ( isPropertyListening() )
188 stopPropertyListening( Reference
< XPropertySet
>( _rSource
.Source
, UNO_QUERY
) );
191 void OAutoConnectionDisposer::clearConnection()
195 // dispose the old connection
196 Reference
< XComponent
> xComp(m_xOriginalConnection
, UNO_QUERY
);
199 m_xOriginalConnection
.clear();
203 OSL_FAIL("OAutoConnectionDisposer::clearConnection: caught an exception!");
207 void SAL_CALL
OAutoConnectionDisposer::cursorMoved( const ::com::sun::star::lang::EventObject
& /*event*/ ) throw (::com::sun::star::uno::RuntimeException
, std::exception
)
211 void SAL_CALL
OAutoConnectionDisposer::rowChanged( const ::com::sun::star::lang::EventObject
& /*event*/ ) throw (::com::sun::star::uno::RuntimeException
, std::exception
)
215 void SAL_CALL
OAutoConnectionDisposer::rowSetChanged( const ::com::sun::star::lang::EventObject
& /*event*/ ) throw (::com::sun::star::uno::RuntimeException
, std::exception
)
217 stopRowSetListening();
224 } // namespace dbtools
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */