From 9954f77b2ecd2591a904dac23aee5e8395334180 Mon Sep 17 00:00:00 2001 From: Michael Stahl Date: Wed, 29 Apr 2015 15:49:46 +0200 Subject: [PATCH] rhbz#1213173: connectivity: Calc driver: prevent document being disposed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ... by adding a XCloseListener that vetoes any attempt to close it. The Calc document can be opened by the user in the UI and closed again. (cherry picked from commit 7368b6ca3f61e750765f42e97d0a00e10fcac516) Conflicts: unotools/source/misc/closeveto.cxx Change-Id: Ied427b67274d925c911e516c0a50a4c0b2b18db9 Reviewed-on: https://gerrit.libreoffice.org/15567 Reviewed-by: Caolán McNamara Tested-by: Caolán McNamara (cherry picked from commit e8777783db81279a6b0e9cc7b308f10719bd8a7b) --- connectivity/source/drivers/calc/CConnection.cxx | 10 ++++++++-- connectivity/source/inc/calc/CConnection.hxx | 10 +++++++--- include/unotools/closeveto.hxx | 3 ++- unotools/source/misc/closeveto.cxx | 17 +++++++++-------- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/connectivity/source/drivers/calc/CConnection.cxx b/connectivity/source/drivers/calc/CConnection.cxx index b81a585..731c4a3 100644 --- a/connectivity/source/drivers/calc/CConnection.cxx +++ b/connectivity/source/drivers/calc/CConnection.cxx @@ -31,6 +31,7 @@ #include "calc/CPreparedStatement.hxx" #include "calc/CStatement.hxx" #include +#include #include #include #include @@ -165,13 +166,17 @@ Reference< XSpreadsheetDocument> OCalcConnection::acquireDoc() ::dbtools::throwGenericSQLException( sError, *this, aErrorDetails ); } osl_atomic_increment(&m_nDocCount); + m_pCloseListener.reset(new utl::CloseVeto(m_xDoc, true)); return m_xDoc; } void OCalcConnection::releaseDoc() { if ( osl_atomic_decrement(&m_nDocCount) == 0 ) - ::comphelper::disposeComponent( m_xDoc ); + { + m_pCloseListener.reset(); // dispose m_xDoc + m_xDoc.clear(); + } } void OCalcConnection::disposing() @@ -179,7 +184,8 @@ void OCalcConnection::disposing() ::osl::MutexGuard aGuard(m_aMutex); m_nDocCount = 0; - ::comphelper::disposeComponent( m_xDoc ); + m_pCloseListener.reset(); // dispose m_xDoc + m_xDoc.clear(); OConnection::disposing(); } diff --git a/connectivity/source/inc/calc/CConnection.hxx b/connectivity/source/inc/calc/CConnection.hxx index b66d892..14e8580 100644 --- a/connectivity/source/inc/calc/CConnection.hxx +++ b/connectivity/source/inc/calc/CConnection.hxx @@ -23,9 +23,11 @@ #include "file/FConnection.hxx" #include -namespace com { namespace sun { namespace star { namespace sheet { - class XSpreadsheetDocument; -} } } } +namespace com { namespace sun { namespace star { + namespace sheet { class XSpreadsheetDocument; } +} } } + +namespace utl { class CloseVeto; } namespace connectivity @@ -37,6 +39,8 @@ namespace connectivity { // the spreadsheet document: ::com::sun::star::uno::Reference< ::com::sun::star::sheet::XSpreadsheetDocument > m_xDoc; + /// close listener that vetoes so nobody disposes m_xDoc + ::std::unique_ptr< ::utl::CloseVeto> m_pCloseListener; OUString m_sPassword; OUString m_aFileName; oslInterlockedCount m_nDocCount; diff --git a/include/unotools/closeveto.hxx b/include/unotools/closeveto.hxx index 9beb0b5..9ed27a7 100644 --- a/include/unotools/closeveto.hxx +++ b/include/unotools/closeveto.hxx @@ -41,7 +41,8 @@ namespace utl class UNOTOOLS_DLLPUBLIC CloseVeto { public: - CloseVeto( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& i_closeable ); + CloseVeto( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& i_closeable, + bool bHasOwnership = false); ~CloseVeto(); private: diff --git a/unotools/source/misc/closeveto.cxx b/unotools/source/misc/closeveto.cxx index 4044fa7..9ecf82e 100644 --- a/unotools/source/misc/closeveto.cxx +++ b/unotools/source/misc/closeveto.cxx @@ -51,8 +51,8 @@ namespace utl class CloseListener_Impl : public CloseListener_Base { public: - CloseListener_Impl() - :m_bHasOwnership( false ) + CloseListener_Impl(bool const bHasOwnership) + : m_bHasOwnership(bHasOwnership) { } @@ -107,12 +107,13 @@ namespace utl namespace { - void lcl_init( CloseVeto_Data& i_data, const Reference< XInterface >& i_closeable ) + void lcl_init( CloseVeto_Data& i_data, const Reference< XInterface >& i_closeable, + bool const hasOwnership) { i_data.xCloseable.set( i_closeable, UNO_QUERY ); ENSURE_OR_RETURN_VOID( i_data.xCloseable.is(), "CloseVeto: the component is not closeable!" ); - i_data.pListener = new CloseListener_Impl; + i_data.pListener = new CloseListener_Impl(hasOwnership); i_data.xCloseable->addCloseListener( i_data.pListener.get() ); } @@ -138,11 +139,11 @@ namespace utl } //= CloseVeto - - CloseVeto::CloseVeto( const Reference< XInterface >& i_closeable ) - :m_pData( new CloseVeto_Data ) + CloseVeto::CloseVeto(const Reference< XInterface >& i_closeable, + bool const hasOwnership) + : m_pData(new CloseVeto_Data) { - lcl_init( *m_pData, i_closeable ); + lcl_init(*m_pData, i_closeable, hasOwnership); } CloseVeto::~CloseVeto() -- 2.1.0