Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / commontools / conncleanup.cxx
blobe01f1770acf5a23eae5324716d333d64134f15c6
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 <osl/diagnose.h>
26 namespace dbtools
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;
36 static const OUString& getActiveConnectionPropertyName()
38 static const OUString s_sActiveConnectionPropertyName( "ActiveConnection" );
39 return s_sActiveConnectionPropertyName;
43 //= OAutoConnectionDisposer
46 OAutoConnectionDisposer::OAutoConnectionDisposer(const Reference< XRowSet >& _rxRowSet, const Reference< XConnection >& _rxConnection)
47 :m_xRowSet( _rxRowSet )
48 ,m_bRSListening( false )
49 ,m_bPropertyListening( false )
51 Reference< XPropertySet > xProps(_rxRowSet, UNO_QUERY);
52 OSL_ENSURE(xProps.is(), "OAutoConnectionDisposer::OAutoConnectionDisposer: invalid rowset (no XPropertySet)!");
54 if (!xProps.is())
55 return;
57 try
59 xProps->setPropertyValue( getActiveConnectionPropertyName(), makeAny( _rxConnection ) );
60 m_xOriginalConnection = _rxConnection;
61 startPropertyListening( xProps );
63 catch( const Exception& )
65 OSL_FAIL( "OAutoConnectionDisposer::OAutoConnectionDisposer: caught an exception!" );
70 void OAutoConnectionDisposer::startPropertyListening( const Reference< XPropertySet >& _rxRowSet )
72 try
74 _rxRowSet->addPropertyChangeListener( getActiveConnectionPropertyName(), this );
75 m_bPropertyListening = true;
77 catch( const Exception& )
79 OSL_FAIL( "OAutoConnectionDisposer::startPropertyListening: caught an exception!" );
84 void OAutoConnectionDisposer::stopPropertyListening( const Reference< XPropertySet >& _rxEventSource )
86 // prevent deletion of ourself while we're herein
87 Reference< XInterface > xKeepAlive(static_cast< XWeak* >(this));
89 try
90 { // remove ourself as property change listener
91 OSL_ENSURE( _rxEventSource.is(), "OAutoConnectionDisposer::stopPropertyListening: invalid event source (no XPropertySet)!" );
92 if ( _rxEventSource.is() )
94 _rxEventSource->removePropertyChangeListener( getActiveConnectionPropertyName(), this );
95 m_bPropertyListening = false;
98 catch( const Exception& )
100 OSL_FAIL( "OAutoConnectionDisposer::stopPropertyListening: caught an exception!" );
105 void OAutoConnectionDisposer::startRowSetListening()
107 OSL_ENSURE( !m_bRSListening, "OAutoConnectionDisposer::startRowSetListening: already listening!" );
110 if ( !m_bRSListening )
111 m_xRowSet->addRowSetListener( this );
113 catch( const Exception& )
115 OSL_FAIL( "OAutoConnectionDisposer::startRowSetListening: caught an exception!" );
117 m_bRSListening = true;
121 void OAutoConnectionDisposer::stopRowSetListening()
123 OSL_ENSURE( m_bRSListening, "OAutoConnectionDisposer::stopRowSetListening: not listening!" );
126 m_xRowSet->removeRowSetListener( this );
128 catch( const Exception& )
130 OSL_FAIL( "OAutoConnectionDisposer::stopRowSetListening: caught an exception!" );
132 m_bRSListening = false;
136 void SAL_CALL OAutoConnectionDisposer::propertyChange( const PropertyChangeEvent& _rEvent ) throw (RuntimeException, std::exception)
138 if ( _rEvent.PropertyName.equals( getActiveConnectionPropertyName() ) )
139 { // somebody set a new ActiveConnection
141 Reference< XConnection > xNewConnection;
142 _rEvent.NewValue >>= xNewConnection;
144 if ( isRowSetListening() )
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 if ( xNewConnection.get() == m_xOriginalConnection.get() )
156 stopRowSetListening();
159 else
161 // start listening at the row set. We're allowed to dispose the old connection as soon
162 // as the RowSet changed
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.
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.
173 // Yes, this is a HACK :(
174 if ( xNewConnection.get() != m_xOriginalConnection.get() )
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 startRowSetListening();
188 void SAL_CALL OAutoConnectionDisposer::disposing( const EventObject& _rSource ) throw (RuntimeException, std::exception)
190 // the rowset is beeing disposed, and nobody has set a new ActiveConnection in the meantime
191 if ( isRowSetListening() )
192 stopRowSetListening();
194 clearConnection();
196 if ( isPropertyListening() )
197 stopPropertyListening( Reference< XPropertySet >( _rSource.Source, UNO_QUERY ) );
200 void OAutoConnectionDisposer::clearConnection()
204 // dispose the old connection
205 Reference< XComponent > xComp(m_xOriginalConnection, UNO_QUERY);
206 if (xComp.is())
207 xComp->dispose();
208 m_xOriginalConnection.clear();
210 catch(Exception&)
212 OSL_FAIL("OAutoConnectionDisposer::clearConnection: caught an exception!");
216 void SAL_CALL OAutoConnectionDisposer::cursorMoved( const ::com::sun::star::lang::EventObject& /*event*/ ) throw (::com::sun::star::uno::RuntimeException, std::exception)
220 void SAL_CALL OAutoConnectionDisposer::rowChanged( const ::com::sun::star::lang::EventObject& /*event*/ ) throw (::com::sun::star::uno::RuntimeException, std::exception)
224 void SAL_CALL OAutoConnectionDisposer::rowSetChanged( const ::com::sun::star::lang::EventObject& /*event*/ ) throw (::com::sun::star::uno::RuntimeException, std::exception)
226 stopRowSetListening();
227 clearConnection();
233 } // namespace dbtools
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */