/src/libreoffice/svx/source/dialog/ClassificationCommon.cxx
Line | Count | Source |
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 | | |
11 | | #include <svx/ClassificationCommon.hxx> |
12 | | #include <svx/ClassificationField.hxx> |
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 | | |
19 | | using namespace css; |
20 | | |
21 | | namespace svx::classification |
22 | | { |
23 | | OUString convertClassificationResultToString(std::vector<svx::ClassificationResult> const& rResults) |
24 | 0 | { |
25 | 0 | OUStringBuffer sRepresentation; |
26 | |
|
27 | 0 | for (svx::ClassificationResult const& rResult : rResults) |
28 | 0 | { |
29 | 0 | switch (rResult.meType) |
30 | 0 | { |
31 | 0 | case svx::ClassificationType::CATEGORY: |
32 | 0 | case svx::ClassificationType::INTELLECTUAL_PROPERTY_PART: |
33 | 0 | case svx::ClassificationType::MARKING: |
34 | 0 | case svx::ClassificationType::TEXT: |
35 | 0 | sRepresentation.append(rResult.msName); |
36 | 0 | break; |
37 | | |
38 | 0 | case svx::ClassificationType::PARAGRAPH: |
39 | 0 | sRepresentation.append(" "); |
40 | 0 | break; |
41 | 0 | } |
42 | 0 | } |
43 | 0 | return sRepresentation.makeStringAndClear(); |
44 | 0 | } |
45 | | |
46 | | OUString getProperty(uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer, |
47 | | OUString const& rName) |
48 | 0 | { |
49 | 0 | try |
50 | 0 | { |
51 | 0 | uno::Reference<beans::XPropertySet> xPropertySet(rxPropertyContainer, uno::UNO_QUERY); |
52 | 0 | return xPropertySet->getPropertyValue(rName).get<OUString>(); |
53 | 0 | } |
54 | 0 | catch (const css::uno::Exception&) |
55 | 0 | { |
56 | 0 | } |
57 | | |
58 | 0 | return OUString(); |
59 | 0 | } |
60 | | |
61 | | bool containsProperty(uno::Sequence<beans::Property> const& rProperties, std::u16string_view rName) |
62 | 0 | { |
63 | 0 | return std::any_of(rProperties.begin(), rProperties.end(), |
64 | 0 | [&](const beans::Property& rProperty) { return rProperty.Name == rName; }); |
65 | 0 | } |
66 | | |
67 | | void removeAllProperties(uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer) |
68 | 0 | { |
69 | 0 | uno::Reference<beans::XPropertySet> xPropertySet(rxPropertyContainer, uno::UNO_QUERY); |
70 | 0 | const uno::Sequence<beans::Property> aProperties |
71 | 0 | = xPropertySet->getPropertySetInfo()->getProperties(); |
72 | |
|
73 | 0 | for (const beans::Property& rProperty : aProperties) |
74 | 0 | { |
75 | 0 | rxPropertyContainer->removeProperty(rProperty.Name); |
76 | 0 | } |
77 | 0 | } |
78 | | |
79 | | bool addOrInsertDocumentProperty( |
80 | | uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer, OUString const& rsKey, |
81 | | OUString const& rsValue) |
82 | 0 | { |
83 | 0 | uno::Reference<beans::XPropertySet> xPropertySet(rxPropertyContainer, uno::UNO_QUERY); |
84 | |
|
85 | 0 | try |
86 | 0 | { |
87 | 0 | if (containsProperty(xPropertySet->getPropertySetInfo()->getProperties(), rsKey)) |
88 | 0 | xPropertySet->setPropertyValue(rsKey, uno::Any(rsValue)); |
89 | 0 | else |
90 | 0 | rxPropertyContainer->addProperty(rsKey, beans::PropertyAttribute::REMOVABLE, |
91 | 0 | uno::Any(rsValue)); |
92 | 0 | } |
93 | 0 | catch (const uno::Exception& /*rException*/) |
94 | 0 | { |
95 | 0 | return false; |
96 | 0 | } |
97 | 0 | return true; |
98 | 0 | } |
99 | | |
100 | | void insertFullTextualRepresentationAsDocumentProperty( |
101 | | uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer, |
102 | | sfx::ClassificationKeyCreator const& rKeyCreator, |
103 | | std::vector<svx::ClassificationResult> const& rResults) |
104 | 0 | { |
105 | 0 | OUString sString = convertClassificationResultToString(rResults); |
106 | 0 | addOrInsertDocumentProperty(rxPropertyContainer, rKeyCreator.makeFullTextualRepresentationKey(), |
107 | 0 | sString); |
108 | 0 | } |
109 | | |
110 | | void insertCreationOrigin(uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer, |
111 | | sfx::ClassificationKeyCreator const& rKeyCreator, |
112 | | sfx::ClassificationCreationOrigin eOrigin) |
113 | 0 | { |
114 | | // Nothing to do if origin is "NONE" |
115 | 0 | if (eOrigin == sfx::ClassificationCreationOrigin::NONE) |
116 | 0 | return; |
117 | | |
118 | 0 | OUString sValue = (eOrigin == sfx::ClassificationCreationOrigin::BAF_POLICY) |
119 | 0 | ? u"BAF_POLICY"_ustr |
120 | 0 | : u"MANUAL"_ustr; |
121 | 0 | addOrInsertDocumentProperty(rxPropertyContainer, rKeyCreator.makeCreationOriginKey(), sValue); |
122 | 0 | } |
123 | | } // end svx::classification namespace |
124 | | |
125 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |