1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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>
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
);
38 case svx::ClassificationType::PARAGRAPH
:
39 sRepresentation
.append(" ");
43 return sRepresentation
.makeStringAndClear();
46 OUString
getProperty(uno::Reference
<beans::XPropertyContainer
> const& rxPropertyContainer
,
47 OUString
const& rName
)
51 uno::Reference
<beans::XPropertySet
> xPropertySet(rxPropertyContainer
, uno::UNO_QUERY
);
52 return xPropertySet
->getPropertyValue(rName
).get
<OUString
>();
54 catch (const css::uno::Exception
&)
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
);
87 if (containsProperty(xPropertySet
->getPropertySetInfo()->getProperties(), rsKey
))
88 xPropertySet
->setPropertyValue(rsKey
, uno::Any(rsValue
));
90 rxPropertyContainer
->addProperty(rsKey
, beans::PropertyAttribute::REMOVABLE
,
93 catch (const uno::Exception
& /*rException*/)
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(),
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
)
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: */