Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / dialog / ClassificationCommon.cxx
blob885dff644e9b008f7960f7be343b77801414b7a6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
11 #include <svx/ClassificationCommon.hxx>
12 #include <svx/ClassificationField.hxx>
13 #include <rtl/ustrbuf.hxx>
15 #include <com/sun/star/beans/PropertyAttribute.hpp>
16 #include <com/sun/star/beans/XPropertySet.hpp>
17 #include <com/sun/star/beans/XPropertyContainer.hpp>
19 using namespace css;
21 namespace svx::classification
23 OUString convertClassificationResultToString(std::vector<svx::ClassificationResult> const& rResults)
25 OUStringBuffer sRepresentation;
27 for (svx::ClassificationResult const& rResult : rResults)
29 switch (rResult.meType)
31 case svx::ClassificationType::CATEGORY:
32 case svx::ClassificationType::INTELLECTUAL_PROPERTY_PART:
33 case svx::ClassificationType::MARKING:
34 case svx::ClassificationType::TEXT:
35 sRepresentation.append(rResult.msName);
36 break;
38 case svx::ClassificationType::PARAGRAPH:
39 sRepresentation.append(" ");
40 break;
43 return sRepresentation.makeStringAndClear();
46 OUString getProperty(uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer,
47 OUString const& rName)
49 try
51 uno::Reference<beans::XPropertySet> xPropertySet(rxPropertyContainer, uno::UNO_QUERY);
52 return xPropertySet->getPropertyValue(rName).get<OUString>();
54 catch (const css::uno::Exception&)
58 return OUString();
61 bool containsProperty(uno::Sequence<beans::Property> const& rProperties, std::u16string_view rName)
63 return std::any_of(rProperties.begin(), rProperties.end(),
64 [&](const beans::Property& rProperty) { return rProperty.Name == rName; });
67 void removeAllProperties(uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer)
69 uno::Reference<beans::XPropertySet> xPropertySet(rxPropertyContainer, uno::UNO_QUERY);
70 const uno::Sequence<beans::Property> aProperties
71 = xPropertySet->getPropertySetInfo()->getProperties();
73 for (const beans::Property& rProperty : aProperties)
75 rxPropertyContainer->removeProperty(rProperty.Name);
79 bool addOrInsertDocumentProperty(
80 uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer, OUString const& rsKey,
81 OUString const& rsValue)
83 uno::Reference<beans::XPropertySet> xPropertySet(rxPropertyContainer, uno::UNO_QUERY);
85 try
87 if (containsProperty(xPropertySet->getPropertySetInfo()->getProperties(), rsKey))
88 xPropertySet->setPropertyValue(rsKey, uno::Any(rsValue));
89 else
90 rxPropertyContainer->addProperty(rsKey, beans::PropertyAttribute::REMOVABLE,
91 uno::Any(rsValue));
93 catch (const uno::Exception& /*rException*/)
95 return false;
97 return true;
100 void insertFullTextualRepresentationAsDocumentProperty(
101 uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer,
102 sfx::ClassificationKeyCreator const& rKeyCreator,
103 std::vector<svx::ClassificationResult> const& rResults)
105 OUString sString = convertClassificationResultToString(rResults);
106 addOrInsertDocumentProperty(rxPropertyContainer, rKeyCreator.makeFullTextualRepresentationKey(),
107 sString);
110 void insertCreationOrigin(uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer,
111 sfx::ClassificationKeyCreator const& rKeyCreator,
112 sfx::ClassificationCreationOrigin eOrigin)
114 // Nothing to do if origin is "NONE"
115 if (eOrigin == sfx::ClassificationCreationOrigin::NONE)
116 return;
118 OUString sValue = (eOrigin == sfx::ClassificationCreationOrigin::BAF_POLICY)
119 ? OUString("BAF_POLICY")
120 : OUString("MANUAL");
121 addOrInsertDocumentProperty(rxPropertyContainer, rKeyCreator.makeCreationOriginKey(), sValue);
123 } // end svx::classification namespace
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */