Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/comphelper/source/misc/configurationhelper.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
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include <comphelper/configurationhelper.hxx>
21
#include <comphelper/sequence.hxx>
22
#include <com/sun/star/beans/XPropertySet.hpp>
23
#include <com/sun/star/beans/PropertyValue.hpp>
24
#include <com/sun/star/configuration/theDefaultProvider.hpp>
25
#include <com/sun/star/container/XNameAccess.hpp>
26
#include <com/sun/star/container/XNameContainer.hpp>
27
#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
28
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
29
#include <com/sun/star/util/XChangesBatch.hpp>
30
31
32
namespace comphelper{
33
34
35
css::uno::Reference< css::uno::XInterface > ConfigurationHelper::openConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
36
                                                                            const OUString&                                           sPackage,
37
                                                                                  EConfigurationModes                                 eMode   )
38
8.06k
{
39
8.06k
    css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(
40
8.06k
        css::configuration::theDefaultProvider::get( rxContext ) );
41
42
8.06k
    std::vector< css::uno::Any > lParams;
43
8.06k
    css::beans::PropertyValue      aParam ;
44
45
    // set root path
46
8.06k
    aParam.Name    = "nodepath";
47
8.06k
    aParam.Value <<= sPackage;
48
8.06k
    lParams.emplace_back(aParam);
49
50
    // enable all locales mode
51
8.06k
    if (eMode & EConfigurationModes::AllLocales)
52
0
    {
53
0
        aParam.Name    = "locale";
54
0
        aParam.Value <<= u"*"_ustr;
55
0
        lParams.emplace_back(aParam);
56
0
    }
57
58
    // open it
59
8.06k
    css::uno::Reference< css::uno::XInterface > xCFG;
60
61
8.06k
    bool bReadOnly(eMode & EConfigurationModes::ReadOnly);
62
8.06k
    if (bReadOnly)
63
8.06k
        xCFG = xConfigProvider->createInstanceWithArguments(
64
8.06k
                u"com.sun.star.configuration.ConfigurationAccess"_ustr,
65
8.06k
                comphelper::containerToSequence(lParams));
66
8
    else
67
8
        xCFG = xConfigProvider->createInstanceWithArguments(
68
8
                u"com.sun.star.configuration.ConfigurationUpdateAccess"_ustr,
69
8
                comphelper::containerToSequence(lParams));
70
71
8.06k
    return xCFG;
72
8.06k
}
73
74
75
css::uno::Any ConfigurationHelper::readRelativeKey(const css::uno::Reference< css::uno::XInterface >& xCFG    ,
76
                                                   const OUString&                            sRelPath,
77
                                                   const OUString&                            sKey    )
78
9
{
79
9
    css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
80
81
9
    css::uno::Reference< css::beans::XPropertySet > xProps;
82
9
    xAccess->getByHierarchicalName(sRelPath) >>= xProps;
83
9
    if (!xProps.is())
84
0
    {
85
0
        throw css::container::NoSuchElementException(
86
0
            "The requested path \"" + sRelPath + "\" does not exist.");
87
0
    }
88
9
    return xProps->getPropertyValue(sKey);
89
9
}
90
91
92
void ConfigurationHelper::writeRelativeKey(const css::uno::Reference< css::uno::XInterface >& xCFG    ,
93
                                           const OUString&                            sRelPath,
94
                                           const OUString&                            sKey    ,
95
                                           const css::uno::Any&                              aValue  )
96
0
{
97
0
    css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
98
99
0
    css::uno::Reference< css::beans::XPropertySet > xProps;
100
0
    xAccess->getByHierarchicalName(sRelPath) >>= xProps;
101
0
    if (!xProps.is())
102
0
    {
103
0
        throw css::container::NoSuchElementException(
104
0
            "The requested path \"" + sRelPath + "\" does not exist.");
105
0
    }
106
0
    xProps->setPropertyValue(sKey, aValue);
107
0
}
108
109
110
css::uno::Reference< css::uno::XInterface > ConfigurationHelper::makeSureSetNodeExists(const css::uno::Reference< css::uno::XInterface >& xCFG         ,
111
                                                                                       const OUString&                            sRelPathToSet,
112
                                                                                       const OUString&                            sSetNode     )
113
17.0k
{
114
17.0k
    css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
115
17.0k
    css::uno::Reference< css::container::XNameAccess > xSet;
116
17.0k
    xAccess->getByHierarchicalName(sRelPathToSet) >>= xSet;
117
17.0k
    if (!xSet.is())
118
0
    {
119
0
        throw css::container::NoSuchElementException(
120
0
            "The requested path \"" + sRelPathToSet + "\" does not exist." );
121
0
    }
122
123
17.0k
    css::uno::Reference< css::uno::XInterface > xNode;
124
17.0k
    if (xSet->hasByName(sSetNode))
125
0
        xSet->getByName(sSetNode) >>= xNode;
126
17.0k
    else
127
17.0k
    {
128
17.0k
        css::uno::Reference< css::lang::XSingleServiceFactory > xNodeFactory(xSet, css::uno::UNO_QUERY_THROW);
129
17.0k
        xNode = xNodeFactory->createInstance();
130
17.0k
        css::uno::Reference< css::container::XNameContainer > xSetReplace(xSet, css::uno::UNO_QUERY_THROW);
131
17.0k
        xSetReplace->insertByName(sSetNode, css::uno::Any(xNode));
132
17.0k
    }
133
134
17.0k
    return xNode;
135
17.0k
}
136
137
138
css::uno::Any ConfigurationHelper::readDirectKey(const css::uno::Reference< css::uno::XComponentContext >&    rxContext,
139
                                                 const OUString&                                       sPackage,
140
                                                 const OUString&                                       sRelPath,
141
                                                 const OUString&                                       sKey    ,
142
                                                       EConfigurationModes                             eMode   )
143
4.26k
{
144
4.26k
    css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(rxContext, sPackage, eMode);
145
4.26k
    return ConfigurationHelper::readRelativeKey(xCFG, sRelPath, sKey);
146
4.26k
}
147
148
149
void ConfigurationHelper::writeDirectKey(const css::uno::Reference< css::uno::XComponentContext >&    rxContext,
150
                                         const OUString&                                       sPackage,
151
                                         const OUString&                                       sRelPath,
152
                                         const OUString&                                       sKey    ,
153
                                         const css::uno::Any&                                  aValue  ,
154
                                               EConfigurationModes                             eMode   )
155
0
{
156
0
    css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(rxContext, sPackage, eMode);
157
0
    ConfigurationHelper::writeRelativeKey(xCFG, sRelPath, sKey, aValue);
158
0
    ConfigurationHelper::flush(xCFG);
159
0
}
160
161
162
void ConfigurationHelper::flush(const css::uno::Reference< css::uno::XInterface >& xCFG)
163
0
{
164
0
    css::uno::Reference< css::util::XChangesBatch > xBatch(xCFG, css::uno::UNO_QUERY_THROW);
165
0
    xBatch->commitChanges();
166
0
}
167
168
} // namespace comphelper
169
170
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */