From ad4b72a0eacf6bb6887236ea3a3807b1264de822 Mon Sep 17 00:00:00 2001 From: Oliver Specht Date: Thu, 21 Mar 2024 12:10:40 +0100 Subject: [PATCH] tdf#160301 import DOCVARIABLE fields as user fields DOCVARIBLE fields in *.doc files are imported as user fields. Change-Id: Ib723d8a586ca644e0b158f839caef33b2b6225a0 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165096 Tested-by: Jenkins Tested-by: Gabor Kelemen Reviewed-by: Thorsten Behrens --- sw/inc/usrfld.hxx | 2 +- sw/source/filter/ww8/ww8par.cxx | 43 +++++++++++++++++++------------- sw/source/filter/ww8/ww8par5.cxx | 30 +++++++++++++++++----- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/sw/inc/usrfld.hxx b/sw/inc/usrfld.hxx index 28d582c4c5ac..f7371ef52c6e 100644 --- a/sw/inc/usrfld.hxx +++ b/sw/inc/usrfld.hxx @@ -102,7 +102,7 @@ inline void SwUserFieldType::SetType(sal_uInt16 nSub) * Tracks the number format and the language, conversion between the float and * string representation is independent from the system locale. */ -class SwUserField final : public SwValueField +class SW_DLLPUBLIC SwUserField final : public SwValueField { sal_uInt16 m_nSubType; diff --git a/sw/source/filter/ww8/ww8par.cxx b/sw/source/filter/ww8/ww8par.cxx index b51239d436ac..77f4f2e76be2 100644 --- a/sw/source/filter/ww8/ww8par.cxx +++ b/sw/source/filter/ww8/ww8par.cxx @@ -27,6 +27,7 @@ #include #include #include +#include #include @@ -4806,27 +4807,35 @@ void SwWW8ImplReader::ReadDocVars() aDocVarStrings, &aDocVarStringIds, &aDocValueStrings); if (m_bVer67) return; - uno::Reference xDPS( - m_pDocShell->GetModel(), uno::UNO_QUERY_THROW); - uno::Reference xDocProps( - xDPS->getDocumentProperties()); - OSL_ENSURE(xDocProps.is(), "DocumentProperties is null"); - uno::Reference xUserDefinedProps = - xDocProps->getUserDefinedProperties(); - OSL_ENSURE(xUserDefinedProps.is(), "UserDefinedProperties is null"); - - for(size_t i=0; i xFieldsSupplier(m_pDocShell->GetModel(), uno::UNO_QUERY_THROW); + uno::Reference xTextFactory(m_pDocShell->GetModel(), uno::UNO_QUERY); + uno::Reference< container::XNameAccess > xFieldMasterAccess = xFieldsSupplier->getTextFieldMasters(); + for(size_t i = 0; i < aDocVarStrings.size(); i++) { const OUString &rName = aDocVarStrings[i]; uno::Any aValue; - aValue <<= rName; - try { - xUserDefinedProps->addProperty( rName, - beans::PropertyAttribute::REMOVABLE, - aValue ); - } catch (const uno::Exception &) { - // ignore + if (aDocValueStrings.size() > i) + { + OUString value = aDocValueStrings[i]; + value = value.replaceAll("\r\n", "\n"); + value = value.replaceAll("\r", "\n"); + aValue <<= value; } + + uno::Reference< beans::XPropertySet > xMaster; + OUString sFieldMasterService("com.sun.star.text.FieldMaster.User." + rName); + + // Find or create Field Master + if (xFieldMasterAccess->hasByName(sFieldMasterService)) + { + xMaster.set(xFieldMasterAccess->getByName(sFieldMasterService), uno::UNO_QUERY_THROW); + } + else + { + xMaster.set(xTextFactory->createInstance("com.sun.star.text.FieldMaster.User"), uno::UNO_QUERY_THROW); + xMaster->setPropertyValue("Name", uno::Any(rName)); + } + xMaster->setPropertyValue("Content", aValue); } } diff --git a/sw/source/filter/ww8/ww8par5.cxx b/sw/source/filter/ww8/ww8par5.cxx index a5b9f881065e..47cb7e788223 100644 --- a/sw/source/filter/ww8/ww8par5.cxx +++ b/sw/source/filter/ww8/ww8par5.cxx @@ -58,6 +58,7 @@ #include #include #include +#include #include #include #include @@ -1830,12 +1831,29 @@ eF_ResT SwWW8ImplReader::Read_F_DocInfo( WW8FieldDesc* pF, OUString& rStr ) aData = aData.replaceAll("\"", ""); } - const auto pType(static_cast( - m_rDoc.getIDocumentFieldsAccess().GetSysFieldType(SwFieldIds::DocInfo))); - SwDocInfoField aField(pType, nSub|nReg, aData, GetFieldResult(pF), nFormat); - if (bDateTime) - ForceFieldLanguage(aField, nLang); - m_rDoc.getIDocumentContentOperations().InsertPoolItem(*m_pPaM, SwFormatField(aField)); + bool bDone = false; + if (DI_CUSTOM == nSub) + { + const auto pType(static_cast( + m_rDoc.getIDocumentFieldsAccess().GetFieldType(SwFieldIds::User, aData, false))); + if (pType) + { + SwUserField aField(pType, 0, nFormat); + if (bDateTime) + ForceFieldLanguage(aField, nLang); + m_rDoc.getIDocumentContentOperations().InsertPoolItem(*m_pPaM, SwFormatField(aField)); + bDone = true; + } + } + if (!bDone) + { + const auto pType(static_cast( + m_rDoc.getIDocumentFieldsAccess().GetSysFieldType(SwFieldIds::DocInfo))); + SwDocInfoField aField(pType, nSub|nReg, aData, GetFieldResult(pF), nFormat); + if (bDateTime) + ForceFieldLanguage(aField, nLang); + m_rDoc.getIDocumentContentOperations().InsertPoolItem(*m_pPaM, SwFormatField(aField)); + } return eF_ResT::OK; }