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 <gabor.kelemen.extern@allotropia.de>
Reviewed-by: Thorsten Behrens <thorsten.behrens@allotropia.de>
This commit is contained in:
Oliver Specht 2024-03-21 12:10:40 +01:00 committed by Thorsten Behrens
parent 64909a84a3
commit ad4b72a0ea
3 changed files with 51 additions and 24 deletions

View file

@ -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;

View file

@ -27,6 +27,7 @@
#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/packages/XPackageEncryption.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/text/XTextFieldsSupplier.hpp>
#include <i18nlangtag/languagetag.hxx>
@ -4806,27 +4807,35 @@ void SwWW8ImplReader::ReadDocVars()
aDocVarStrings, &aDocVarStringIds, &aDocValueStrings);
if (m_bVer67) return;
uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
m_pDocShell->GetModel(), uno::UNO_QUERY_THROW);
uno::Reference<document::XDocumentProperties> xDocProps(
xDPS->getDocumentProperties());
OSL_ENSURE(xDocProps.is(), "DocumentProperties is null");
uno::Reference<beans::XPropertyContainer> xUserDefinedProps =
xDocProps->getUserDefinedProperties();
OSL_ENSURE(xUserDefinedProps.is(), "UserDefinedProperties is null");
for(size_t i=0; i<aDocVarStrings.size(); i++)
uno::Reference< text::XTextFieldsSupplier > xFieldsSupplier(m_pDocShell->GetModel(), uno::UNO_QUERY_THROW);
uno::Reference<css::lang::XMultiServiceFactory> 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);
}
}

View file

@ -58,6 +58,7 @@
#include <IDocumentState.hxx>
#include <flddat.hxx>
#include <docufld.hxx>
#include <usrfld.hxx>
#include <reffld.hxx>
#include <IMark.hxx>
#include <expfld.hxx>
@ -1830,12 +1831,29 @@ eF_ResT SwWW8ImplReader::Read_F_DocInfo( WW8FieldDesc* pF, OUString& rStr )
aData = aData.replaceAll("\"", "");
}
const auto pType(static_cast<SwDocInfoFieldType*>(
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<SwUserFieldType*>(
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<SwDocInfoFieldType*>(
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;
}