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