LibreOffice Module svx (master) 1
ClassificationCommon.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 */
10
13#include <rtl/ustrbuf.hxx>
14
15#include <com/sun/star/beans/PropertyAttribute.hpp>
16#include <com/sun/star/beans/XPropertySet.hpp>
17#include <com/sun/star/beans/XPropertyContainer.hpp>
18
19using namespace css;
20
22{
23OUString convertClassificationResultToString(std::vector<svx::ClassificationResult> const& rResults)
24{
25 OUStringBuffer sRepresentation;
26
27 for (svx::ClassificationResult const& rResult : rResults)
28 {
29 switch (rResult.meType)
30 {
35 sRepresentation.append(rResult.msName);
36 break;
37
39 sRepresentation.append(" ");
40 break;
41 }
42 }
43 return sRepresentation.makeStringAndClear();
44}
45
46OUString getProperty(uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer,
47 OUString const& rName)
48{
49 try
50 {
51 uno::Reference<beans::XPropertySet> xPropertySet(rxPropertyContainer, uno::UNO_QUERY);
52 return xPropertySet->getPropertyValue(rName).get<OUString>();
53 }
54 catch (const css::uno::Exception&)
55 {
56 }
57
58 return OUString();
59}
60
61bool containsProperty(uno::Sequence<beans::Property> const& rProperties, std::u16string_view rName)
62{
63 return std::any_of(rProperties.begin(), rProperties.end(),
64 [&](const beans::Property& rProperty) { return rProperty.Name == rName; });
65}
66
67void removeAllProperties(uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer)
68{
69 uno::Reference<beans::XPropertySet> xPropertySet(rxPropertyContainer, uno::UNO_QUERY);
70 const uno::Sequence<beans::Property> aProperties
71 = xPropertySet->getPropertySetInfo()->getProperties();
72
73 for (const beans::Property& rProperty : aProperties)
74 {
75 rxPropertyContainer->removeProperty(rProperty.Name);
76 }
77}
78
80 uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer, OUString const& rsKey,
81 OUString const& rsValue)
82{
83 uno::Reference<beans::XPropertySet> xPropertySet(rxPropertyContainer, uno::UNO_QUERY);
84
85 try
86 {
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));
92 }
93 catch (const uno::Exception& /*rException*/)
94 {
95 return false;
96 }
97 return true;
98}
99
101 uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer,
102 sfx::ClassificationKeyCreator const& rKeyCreator,
103 std::vector<svx::ClassificationResult> const& rResults)
104{
105 OUString sString = convertClassificationResultToString(rResults);
106 addOrInsertDocumentProperty(rxPropertyContainer, rKeyCreator.makeFullTextualRepresentationKey(),
107 sString);
108}
109
110void insertCreationOrigin(uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer,
111 sfx::ClassificationKeyCreator const& rKeyCreator,
113{
114 // Nothing to do if origin is "NONE"
116 return;
117
118 OUString sValue = (eOrigin == sfx::ClassificationCreationOrigin::BAF_POLICY)
119 ? OUString("BAF_POLICY")
120 : OUString("MANUAL");
121 addOrInsertDocumentProperty(rxPropertyContainer, rKeyCreator.makeCreationOriginKey(), sValue);
122}
123} // end svx::classification namespace
124
125/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertiesInfo aProperties
OUString makeCreationOriginKey() const
OUString makeFullTextualRepresentationKey() const
ClassificationCreationOrigin
bool containsProperty(uno::Sequence< beans::Property > const &rProperties, std::u16string_view rName)
OUString convertClassificationResultToString(std::vector< svx::ClassificationResult > const &rResults)
OUString getProperty(uno::Reference< beans::XPropertyContainer > const &rxPropertyContainer, OUString const &rName)
void insertFullTextualRepresentationAsDocumentProperty(uno::Reference< beans::XPropertyContainer > const &rxPropertyContainer, sfx::ClassificationKeyCreator const &rKeyCreator, std::vector< svx::ClassificationResult > const &rResults)
void insertCreationOrigin(uno::Reference< beans::XPropertyContainer > const &rxPropertyContainer, sfx::ClassificationKeyCreator const &rKeyCreator, sfx::ClassificationCreationOrigin eOrigin)
bool addOrInsertDocumentProperty(uno::Reference< beans::XPropertyContainer > const &rxPropertyContainer, OUString const &rsKey, OUString const &rsValue)
void removeAllProperties(uno::Reference< beans::XPropertyContainer > const &rxPropertyContainer)