bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / misc / configuration.cxx
blobb01bdaef477bc8e2becff7ff523da99d6ed0989b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <sal/config.h>
12 #include <cassert>
13 #include <memory>
15 #include <com/sun/star/beans/PropertyAttribute.hpp>
16 #include <com/sun/star/configuration/ReadOnlyAccess.hpp>
17 #include <com/sun/star/configuration/ReadWriteAccess.hpp>
18 #include <com/sun/star/configuration/XReadWriteAccess.hpp>
19 #include <com/sun/star/configuration/theDefaultProvider.hpp>
20 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
21 #include <com/sun/star/container/XHierarchicalNameReplace.hpp>
22 #include <com/sun/star/container/XNameAccess.hpp>
23 #include <com/sun/star/container/XNameContainer.hpp>
24 #include <com/sun/star/lang/Locale.hpp>
25 #include <com/sun/star/lang/XLocalizable.hpp>
26 #include <com/sun/star/uno/Any.hxx>
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <com/sun/star/uno/XComponentContext.hpp>
29 #include <comphelper/configuration.hxx>
30 #include <rtl/instance.hxx>
31 #include <rtl/ustrbuf.hxx>
32 #include <rtl/ustring.hxx>
33 #include <sal/log.hxx>
34 #include <i18nlangtag/languagetag.hxx>
36 namespace {
38 struct TheConfigurationWrapper:
39 public rtl::StaticWithArg<
40 comphelper::detail::ConfigurationWrapper,
41 css::uno::Reference< css::uno::XComponentContext >,
42 TheConfigurationWrapper >
43 {};
45 OUString getDefaultLocale(
46 css::uno::Reference< css::uno::XComponentContext > const & context)
48 return LanguageTag(
49 css::uno::Reference< css::lang::XLocalizable >(
50 css::configuration::theDefaultProvider::get(context),
51 css::uno::UNO_QUERY_THROW)->
52 getLocale()).getBcp47(false);
55 OUString extendLocalizedPath(OUString const & path, OUString const & locale) {
56 OUStringBuffer buf(path);
57 buf.append("/['*");
58 SAL_WARN_IF(
59 locale.match("*"), "comphelper",
60 "Locale \"" << locale << "\" starts with \"*\"");
61 assert(locale.indexOf('&') == -1);
62 assert(locale.indexOf('"') == -1);
63 assert(locale.indexOf('\'') == -1);
64 buf.append(locale);
65 buf.append("']");
66 return buf.makeStringAndClear();
71 std::shared_ptr< comphelper::ConfigurationChanges >
72 comphelper::ConfigurationChanges::create(
73 css::uno::Reference< css::uno::XComponentContext > const & context)
75 return TheConfigurationWrapper::get(context).createChanges();
78 comphelper::ConfigurationChanges::~ConfigurationChanges() {}
80 void comphelper::ConfigurationChanges::commit() const {
81 access_->commitChanges();
84 comphelper::ConfigurationChanges::ConfigurationChanges(
85 css::uno::Reference< css::uno::XComponentContext > const & context):
86 access_(
87 css::configuration::ReadWriteAccess::create(
88 context, getDefaultLocale(context)))
91 void comphelper::ConfigurationChanges::setPropertyValue(
92 OUString const & path, css::uno::Any const & value) const
94 access_->replaceByHierarchicalName(path, value);
97 css::uno::Reference< css::container::XHierarchicalNameReplace >
98 comphelper::ConfigurationChanges::getGroup(OUString const & path) const
100 return css::uno::Reference< css::container::XHierarchicalNameReplace >(
101 access_->getByHierarchicalName(path), css::uno::UNO_QUERY_THROW);
104 css::uno::Reference< css::container::XNameContainer >
105 comphelper::ConfigurationChanges::getSet(OUString const & path) const
107 return css::uno::Reference< css::container::XNameContainer >(
108 access_->getByHierarchicalName(path), css::uno::UNO_QUERY_THROW);
111 comphelper::detail::ConfigurationWrapper const &
112 comphelper::detail::ConfigurationWrapper::get(
113 css::uno::Reference< css::uno::XComponentContext > const & context)
115 return TheConfigurationWrapper::get(context);
118 comphelper::detail::ConfigurationWrapper::ConfigurationWrapper(
119 css::uno::Reference< css::uno::XComponentContext > const & context):
120 context_(context),
121 access_(css::configuration::ReadWriteAccess::create(context, "*"))
124 comphelper::detail::ConfigurationWrapper::~ConfigurationWrapper() {}
126 bool comphelper::detail::ConfigurationWrapper::isReadOnly(OUString const & path)
127 const
129 css::beans::Property SB(access_->getPropertyByHierarchicalName(path));
130 return
131 (access_->getPropertyByHierarchicalName(path).Attributes
132 & css::beans::PropertyAttribute::READONLY)
133 != 0;
136 css::uno::Any comphelper::detail::ConfigurationWrapper::getPropertyValue(
137 OUString const & path) const
139 return access_->getByHierarchicalName(path);
142 void comphelper::detail::ConfigurationWrapper::setPropertyValue(
143 std::shared_ptr< ConfigurationChanges > const & batch,
144 OUString const & path, com::sun::star::uno::Any const & value)
146 assert(batch.get() != 0);
147 batch->setPropertyValue(path, value);
150 css::uno::Any
151 comphelper::detail::ConfigurationWrapper::getLocalizedPropertyValue(
152 OUString const & path) const
154 return access_->getByHierarchicalName(
155 extendLocalizedPath(path, getDefaultLocale(context_)));
158 void comphelper::detail::ConfigurationWrapper::setLocalizedPropertyValue(
159 std::shared_ptr< ConfigurationChanges > const & batch,
160 OUString const & path, com::sun::star::uno::Any const & value)
162 assert(batch.get() != 0);
163 batch->setPropertyValue(path, value);
166 css::uno::Reference< css::container::XHierarchicalNameAccess >
167 comphelper::detail::ConfigurationWrapper::getGroupReadOnly(
168 OUString const & path) const
170 return css::uno::Reference< css::container::XHierarchicalNameAccess >(
171 (css::configuration::ReadOnlyAccess::create(
172 context_, getDefaultLocale(context_))->
173 getByHierarchicalName(path)),
174 css::uno::UNO_QUERY_THROW);
177 css::uno::Reference< css::container::XHierarchicalNameReplace >
178 comphelper::detail::ConfigurationWrapper::getGroupReadWrite(
179 std::shared_ptr< ConfigurationChanges > const & batch,
180 OUString const & path)
182 assert(batch.get() != 0);
183 return batch->getGroup(path);
186 css::uno::Reference< css::container::XNameAccess >
187 comphelper::detail::ConfigurationWrapper::getSetReadOnly(
188 OUString const & path) const
190 return css::uno::Reference< css::container::XNameAccess >(
191 (css::configuration::ReadOnlyAccess::create(
192 context_, getDefaultLocale(context_))->
193 getByHierarchicalName(path)),
194 css::uno::UNO_QUERY_THROW);
197 css::uno::Reference< css::container::XNameContainer >
198 comphelper::detail::ConfigurationWrapper::getSetReadWrite(
199 std::shared_ptr< ConfigurationChanges > const & batch,
200 OUString const & path)
202 assert(batch.get() != 0);
203 return batch->getSet(path);
206 std::shared_ptr< comphelper::ConfigurationChanges >
207 comphelper::detail::ConfigurationWrapper::createChanges() const {
208 return std::shared_ptr< ConfigurationChanges >(
209 new ConfigurationChanges(context_));
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */