From b73c6c8e53cf944848cdb3a79371703de99a8710 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Wed, 28 Apr 2021 09:50:54 +0200 Subject: sw bibliography: fix warning when de-selecting a biblio entry field De-selecting a biblio entry field resulted in this warning: warn:sw.core:3311:3311:sw/source/core/fields/authfld.cxx:104: SwAuthorityFieldType::RemoveField: pEntry is not my field But this was even an assert before commit 64ffabbdb2725e93de997171708bb31c33c93a55 (sw bibliography, refer to a page: make the biblio field clickable, 2021-03-12). It seems the root of the problem was: - SwAuthorityFieldType has a list of SwAuthEntry instances that its SwAuthorityFields may have - when the field is selected, we copy the selection to a clipboard document, using SwFEShell::Copy() - this calls SwAuthorityFieldType::AppendField() to register the SwAuthEntry, but that registers a copy instead - SwAuthorityFieldType::RemoveField() then asserted that the original SwAuthEntry is part of SwAuthorityFieldType's list, but it was not Fix the problem by returning a reference to the copied SwAuthEntry in SwAuthorityFieldType::AppendField(), that fixes the warning and then we can turn this back to an assert, to detect problems where an unregistered SwAuthEntry would be de-registered. In practice this caused a problem in the Insert Bibliography Entry dialog: bibliography source = document content case uses SwAuthorityFieldType::GetAllEntryIdentifiers() to provide a list of sources, and this way sources were not removed from that list when deleting biblio entry fields. Change-Id: Iea4fa44302aaac0daa122bbf227888d1dbb06597 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/114765 Reviewed-by: Miklos Vajna Tested-by: Jenkins --- sw/source/core/fields/authfld.cxx | 12 ++++++------ sw/source/core/fields/fldbas.cxx | 2 ++ sw/source/uibase/utlui/initui.cxx | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) (limited to 'sw/source') diff --git a/sw/source/core/fields/authfld.cxx b/sw/source/core/fields/authfld.cxx index 934f06f1243b..7a2dea545ab9 100644 --- a/sw/source/core/fields/authfld.cxx +++ b/sw/source/core/fields/authfld.cxx @@ -101,7 +101,7 @@ void SwAuthorityFieldType::RemoveField(const SwAuthEntry* pEntry) return; } } - SAL_WARN("sw.core", "SwAuthorityFieldType::RemoveField: pEntry is not my field"); + assert(false && "SwAuthorityFieldType::RemoveField: pEntry was not added previously"); } SwAuthEntry* SwAuthorityFieldType::AddField(const OUString& rFieldContents) @@ -167,18 +167,18 @@ bool SwAuthorityFieldType::ChangeEntryContent(const SwAuthEntry* pNewEntry) return false; } -/// appends a new entry (if new) and returns the array position -sal_uInt16 SwAuthorityFieldType::AppendField( const SwAuthEntry& rInsert ) +/// appends a new entry (if new) and returns the copied entry +SwAuthEntry* SwAuthorityFieldType::AppendField( const SwAuthEntry& rInsert ) { for( SwAuthDataArr::size_type nRet = 0; nRet < m_DataArr.size(); ++nRet ) { if( *m_DataArr[ nRet ] == rInsert ) - return nRet; + return m_DataArr[ nRet ].get(); } //if it is a new Entry - insert m_DataArr.push_back(new SwAuthEntry(rInsert)); - return m_DataArr.size()-1; + return m_DataArr.back().get(); } std::unique_ptr SwAuthorityFieldType::CreateTOXInternational() const @@ -770,7 +770,7 @@ SwFieldType* SwAuthorityField::ChgTyp( SwFieldType* pFieldTyp ) if( pSrcTyp != pDstTyp ) { const SwAuthEntry* pSrcEntry = m_xAuthEntry.get(); - pDstTyp->AppendField( *pSrcEntry ); + m_xAuthEntry = pDstTyp->AppendField( *pSrcEntry ); pSrcTyp->RemoveField( pSrcEntry ); SwField::ChgTyp( pFieldTyp ); } diff --git a/sw/source/core/fields/fldbas.cxx b/sw/source/core/fields/fldbas.cxx index 2d824ac52c28..cf3a41460ba3 100644 --- a/sw/source/core/fields/fldbas.cxx +++ b/sw/source/core/fields/fldbas.cxx @@ -163,6 +163,8 @@ void SwFieldType::dumpAsXml(xmlTextWriterPtr pWriter) const if(!vFields.size()) return; (void)xmlTextWriterStartElement(pWriter, BAD_CAST("SwFieldType")); + (void)xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("ptr"), "%p", this); + (void)xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("symbol"), "%s", BAD_CAST(typeid(*this).name())); for(const auto pFormatField: vFields) pFormatField->dumpAsXml(pWriter); (void)xmlTextWriterEndElement(pWriter); diff --git a/sw/source/uibase/utlui/initui.cxx b/sw/source/uibase/utlui/initui.cxx index f9567ec4ab45..a690b7cfe9df 100644 --- a/sw/source/uibase/utlui/initui.cxx +++ b/sw/source/uibase/utlui/initui.cxx @@ -17,6 +17,8 @@ * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ +#include + #include #include #include @@ -286,4 +288,21 @@ OUString const & SwAuthorityFieldType::GetAuthTypeName(ToxAuthorityType eType) return (*pAuthFieldTypeList)[static_cast< sal_uInt16 >(eType)]; } +void SwAuthorityFieldType::dumpAsXml(xmlTextWriterPtr pWriter) const +{ + (void)xmlTextWriterStartElement(pWriter, BAD_CAST("SwAuthorityFieldType")); + SwFieldType::dumpAsXml(pWriter); + + (void)xmlTextWriterStartElement(pWriter, BAD_CAST("DataArr")); + for (const auto& xAuthEntry : m_DataArr) + { + (void)xmlTextWriterStartElement(pWriter, BAD_CAST("AuthEntry")); + (void)xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("ptr"), "%p", xAuthEntry.get()); + (void)xmlTextWriterEndElement(pWriter); + } + (void)xmlTextWriterEndElement(pWriter); + + (void)xmlTextWriterEndElement(pWriter); +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3