From 29bd193146b40cf90bda5db04b136987f3c7c94d Mon Sep 17 00:00:00 2001 From: Szymon Kłos Date: Sat, 16 Sep 2017 17:01:08 +0200 Subject: tdf#108572 remove connection also if not saved MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Iddce37c3ad187f4a5572cb3cc2362535134c28e8 Reviewed-on: https://gerrit.libreoffice.org/42357 Tested-by: Jenkins Reviewed-by: Szymon Kłos --- sw/inc/dbmgr.hxx | 9 +++++++++ sw/source/core/doc/docnew.cxx | 7 +++++++ sw/source/uibase/dbui/dbmgr.cxx | 33 ++++++++++++++++++++++++++++----- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/sw/inc/dbmgr.hxx b/sw/inc/dbmgr.hxx index b0da6613add0..0947bf0e4804 100644 --- a/sw/inc/dbmgr.hxx +++ b/sw/inc/dbmgr.hxx @@ -259,6 +259,12 @@ class SW_DLLPUBLIC SwDBManager /// Store last registrations to revoke or commit static std::vector> m_aUncommitedRegistrations; + /// Not used connections. + std::vector m_aNotUsedConnections; + + /// Set connection as used. + void SetAsUsed(const OUString& rName); + /// The document that owns this manager. SwDoc* m_pDoc; @@ -485,6 +491,9 @@ public: /// Accept not commited registrations void CommitLastRegistrations(); + + /// Remove not used connections. + void RevokeNotUsedConnections(); }; #endif diff --git a/sw/source/core/doc/docnew.cxx b/sw/source/core/doc/docnew.cxx index 5a53fc42f25a..86e5085692c8 100644 --- a/sw/source/core/doc/docnew.cxx +++ b/sw/source/core/doc/docnew.cxx @@ -525,6 +525,13 @@ SwDoc::~SwDoc() mpDBManager->releaseRevokeListener(); SwDBManager::RevokeDataSource(maDBData.sDataSource); } + else if (!mpDBManager->getEmbeddedName().isEmpty()) + { + // Remove the revoke listener here first, so that we don't remove the data source from the document. + mpDBManager->releaseRevokeListener(); + // Remove connections which was committed but not used. + mpDBManager->RevokeNotUsedConnections(); + } DELETEZ( mpDBManager ); #endif diff --git a/sw/source/uibase/dbui/dbmgr.cxx b/sw/source/uibase/dbui/dbmgr.cxx index 3de0f42ac26b..bfb0a539df57 100644 --- a/sw/source/uibase/dbui/dbmgr.cxx +++ b/sw/source/uibase/dbui/dbmgr.cxx @@ -2517,6 +2517,7 @@ SwDSParam* SwDBManager::FindDSConnection(const OUString& rDataSource, bool bCre //prefer merge data if available if(pImpl->pMergeData && rDataSource == pImpl->pMergeData->sDataSource ) { + SetAsUsed(rDataSource); return pImpl->pMergeData; } SwDSParam* pFound = nullptr; @@ -2524,6 +2525,7 @@ SwDSParam* SwDBManager::FindDSConnection(const OUString& rDataSource, bool bCre { if(rDataSource == pParam->sDataSource) { + SetAsUsed(rDataSource); pFound = pParam.get(); break; } @@ -2533,6 +2535,7 @@ SwDSParam* SwDBManager::FindDSConnection(const OUString& rDataSource, bool bCre SwDBData aData; aData.sDataSource = rDataSource; pFound = new SwDSParam(aData); + SetAsUsed(rDataSource); m_DataSourceParams.push_back(std::unique_ptr(pFound)); try { @@ -3221,12 +3224,32 @@ void SwDBManager::RevokeLastRegistrations() void SwDBManager::CommitLastRegistrations() { - auto predicate = [this](const std::pair& x) - { return x.first == this->m_pDoc->GetDocShell(); }; + for (auto aIt = m_aUncommitedRegistrations.begin(); aIt != m_aUncommitedRegistrations.end();) + { + if (aIt->first == m_pDoc->GetDocShell()) + { + m_aNotUsedConnections.push_back(aIt->second); + aIt = m_aUncommitedRegistrations.erase(aIt); + } + else + aIt++; + } +} - m_aUncommitedRegistrations.erase( - std::remove_if(m_aUncommitedRegistrations.begin(), m_aUncommitedRegistrations.end(), predicate), - m_aUncommitedRegistrations.end()); +void SwDBManager::SetAsUsed(const OUString& rName) +{ + auto aFound = std::find(m_aNotUsedConnections.begin(), m_aNotUsedConnections.end(), rName); + if (aFound != m_aNotUsedConnections.end()) + m_aNotUsedConnections.erase(aFound); +} + +void SwDBManager::RevokeNotUsedConnections() +{ + for (auto aIt = m_aNotUsedConnections.begin(); aIt != m_aNotUsedConnections.end();) + { + RevokeDataSource(*aIt); + aIt = m_aNotUsedConnections.erase(aIt); + } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3