summaryrefslogtreecommitdiff
path: root/svx/source/dialog/ClassificationCommon.cxx
blob: 53200046d3e583423c3e3e49276446b2e2d51f01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

#include <svx/ClassificationCommon.hxx>

#include <com/sun/star/beans/PropertyAttribute.hpp>

using namespace css;

namespace svx
{
namespace classification
{
OUString convertClassificationResultToString(std::vector<svx::ClassificationResult> const& rResults)
{
    OUString sRepresentation = "";

    for (svx::ClassificationResult const& rResult : rResults)
    {
        switch (rResult.meType)
        {
            case svx::ClassificationType::CATEGORY:
            case svx::ClassificationType::INTELLECTUAL_PROPERTY_PART:
            case svx::ClassificationType::MARKING:
            case svx::ClassificationType::TEXT:
                sRepresentation += rResult.msName;
                break;

            case svx::ClassificationType::PARAGRAPH:
                sRepresentation += " ";
                break;
        }
    }
    return sRepresentation;
}

OUString getProperty(uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer,
                     OUString const& rName)
{
    try
    {
        uno::Reference<beans::XPropertySet> xPropertySet(rxPropertyContainer, uno::UNO_QUERY);
        return xPropertySet->getPropertyValue(rName).get<OUString>();
    }
    catch (const css::uno::Exception&)
    {
    }

    return OUString();
}

bool containsProperty(uno::Sequence<beans::Property> const& rProperties, OUString const& rName)
{
    return std::find_if(rProperties.begin(), rProperties.end(),
                        [&](const beans::Property& rProperty) { return rProperty.Name == rName; })
           != rProperties.end();
}

void removeAllProperties(uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer)
{
    uno::Reference<beans::XPropertySet> xPropertySet(rxPropertyContainer, uno::UNO_QUERY);
    uno::Sequence<beans::Property> aProperties
        = xPropertySet->getPropertySetInfo()->getProperties();

    for (const beans::Property& rProperty : aProperties)
    {
        rxPropertyContainer->removeProperty(rProperty.Name);
    }
}

bool addOrInsertDocumentProperty(
    uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer, OUString const& rsKey,
    OUString const& rsValue)
{
    uno::Reference<beans::XPropertySet> xPropertySet(rxPropertyContainer, uno::UNO_QUERY);

    try
    {
        if (containsProperty(xPropertySet->getPropertySetInfo()->getProperties(), rsKey))
            xPropertySet->setPropertyValue(rsKey, uno::makeAny(rsValue));
        else
            rxPropertyContainer->addProperty(rsKey, beans::PropertyAttribute::REMOVABLE,
                                             uno::makeAny(rsValue));
    }
    catch (const uno::Exception& /*rException*/)
    {
        return false;
    }
    return true;
}

void insertFullTextualRepresentationAsDocumentProperty(
    uno::Reference<beans::XPropertyContainer> const& rxPropertyContainer,
    sfx::ClassificationKeyCreator const& rKeyCreator,
    std::vector<svx::ClassificationResult> const& rResults)
{
    OUString sString = convertClassificationResultToString(rResults);
    addOrInsertDocumentProperty(rxPropertyContainer, rKeyCreator.makeFullTextualRepresentationKey(),
                                sString);
}

void insertCreationOrigin(uno::Reference<beans::XPropertyContainer> const & rxPropertyContainer,
                          sfx::ClassificationKeyCreator const & rKeyCreator,
                          sfx::ClassificationCreationOrigin eOrigin)
{
    // Nothing to do if origin is "NONE"
    if (eOrigin == sfx::ClassificationCreationOrigin::NONE)
        return;

    OUString sValue = (eOrigin == sfx::ClassificationCreationOrigin::BAF_POLICY) ? OUString("BAF_POLICY") : OUString("MANUAL");
    addOrInsertDocumentProperty(rxPropertyContainer, rKeyCreator.makeCreationOriginKey(), sValue);
}

sfx::ClassificationCreationOrigin getCreationOriginProperty(uno::Reference<beans::XPropertyContainer> const & rxPropertyContainer,
                                                            sfx::ClassificationKeyCreator const & rKeyCreator)
{
    OUString sValue = getProperty(rxPropertyContainer, rKeyCreator.makeCreationOriginKey());
    if (sValue.isEmpty())
        return sfx::ClassificationCreationOrigin::NONE;

    return (sValue == "BAF_POLICY")
                ? sfx::ClassificationCreationOrigin::BAF_POLICY
                : sfx::ClassificationCreationOrigin::MANUAL;
}

}
} // end svx::classification namespace

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */