diff options
author | Miklos Vajna <vmiklos@collabora.co.uk> | 2016-02-24 12:08:50 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.co.uk> | 2016-02-24 13:45:46 +0100 |
commit | 09fc095dd47e9e1025fc185ed1a10826f481f0cb (patch) | |
tree | f260fef530e62fcaf0fa825932c5928ad72d0b7b /sw/source/core/edit | |
parent | 4461e0541d9fc772984e2cfbe9464ae44563ad57 (diff) |
sw classification header: avoid inserting the field multiple times
If there is a field that's the same we would append, don't do anything.
The document property is already updated, and the rest is automatic:
it's a field after all.
Change-Id: I68713629a6917657ff491646c1b7781a9603e4f2
Diffstat (limited to 'sw/source/core/edit')
-rw-r--r-- | sw/source/core/edit/edfcol.cxx | 54 |
1 files changed, 48 insertions, 6 deletions
diff --git a/sw/source/core/edit/edfcol.cxx b/sw/source/core/edit/edfcol.cxx index 3d8908a72da5..cb8124c07475 100644 --- a/sw/source/core/edit/edfcol.cxx +++ b/sw/source/core/edit/edfcol.cxx @@ -20,6 +20,8 @@ #include <editsh.hxx> #include <com/sun/star/style/XStyleFamiliesSupplier.hpp> +#include <com/sun/star/container/XEnumerationAccess.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> #include <hintids.hxx> #include <editeng/formatbreakitem.hxx> @@ -59,6 +61,41 @@ std::set<OUString> lcl_getUsedPageStyles(SwViewShell* pShell) return aRet; } +/// Search for a field named rFieldName of type rServiceName in xText. +bool lcl_hasField(const uno::Reference<text::XText>& xText, const OUString& rServiceName, const OUString& rFieldName) +{ + uno::Reference<container::XEnumerationAccess> xParagraphEnumerationAccess(xText, uno::UNO_QUERY); + uno::Reference<container::XEnumeration> xParagraphs = xParagraphEnumerationAccess->createEnumeration(); + while (xParagraphs->hasMoreElements()) + { + uno::Reference<container::XEnumerationAccess> xTextPortionEnumerationAccess(xParagraphs->nextElement(), uno::UNO_QUERY); + uno::Reference<container::XEnumeration> xTextPortions = xTextPortionEnumerationAccess->createEnumeration(); + while (xTextPortions->hasMoreElements()) + { + uno::Reference<beans::XPropertySet> xTextPortion(xTextPortions->nextElement(), uno::UNO_QUERY); + OUString aTextPortionType; + xTextPortion->getPropertyValue(UNO_NAME_TEXT_PORTION_TYPE) >>= aTextPortionType; + if (aTextPortionType != UNO_NAME_TEXT_FIELD) + continue; + + uno::Reference<lang::XServiceInfo> xTextField; + xTextPortion->getPropertyValue(UNO_NAME_TEXT_FIELD) >>= xTextField; + if (!xTextField->supportsService(rServiceName)) + continue; + + OUString aName; + uno::Reference<beans::XPropertySet> xPropertySet(xTextField, uno::UNO_QUERY); + xPropertySet->getPropertyValue(UNO_NAME_NAME) >>= aName; + if (aName != rFieldName) + continue; + + return true; + } + } + + return false; +} + } // anonymous namespace SwTextFormatColl& SwEditShell::GetDfltTextFormatColl() const @@ -104,14 +141,19 @@ void SwEditShell::SetClassification(const OUString& rName) if (!bHeaderIsOn) xPageStyle->setPropertyValue(UNO_NAME_HEADER_IS_ON, uno::makeAny(true)); - // Append a field to the end of the header text. - uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory(xModel, uno::UNO_QUERY); - uno::Reference<beans::XPropertySet> xField(xMultiServiceFactory->createInstance("com.sun.star.text.TextField.DocInfo.Custom"), uno::UNO_QUERY); - xField->setPropertyValue(UNO_NAME_NAME, uno::makeAny(SfxClassificationHelper::PROP_DOCHEADER())); + // If the header already contains a document header field, no need to do anything. uno::Reference<text::XText> xHeaderText; xPageStyle->getPropertyValue(UNO_NAME_HEADER_TEXT) >>= xHeaderText; - uno::Reference<text::XTextContent> xTextContent(xField, uno::UNO_QUERY); - xHeaderText->insertTextContent(xHeaderText->getEnd(), xTextContent, /*bAbsorb=*/false); + OUString aServiceName = "com.sun.star.text.TextField.DocInfo.Custom"; + if (!lcl_hasField(xHeaderText, aServiceName, SfxClassificationHelper::PROP_DOCHEADER())) + { + // Append a field to the end of the header text. + uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory(xModel, uno::UNO_QUERY); + uno::Reference<beans::XPropertySet> xField(xMultiServiceFactory->createInstance(aServiceName), uno::UNO_QUERY); + xField->setPropertyValue(UNO_NAME_NAME, uno::makeAny(SfxClassificationHelper::PROP_DOCHEADER())); + uno::Reference<text::XTextContent> xTextContent(xField, uno::UNO_QUERY); + xHeaderText->insertTextContent(xHeaderText->getEnd(), xTextContent, /*bAbsorb=*/false); + } } } } |