Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : : *
5 : : * The contents of this file are subject to the Mozilla Public License Version
6 : : * 1.1 (the "License"); you may not use this file except in compliance with
7 : : * the License or as specified alternatively below. You may obtain a copy of
8 : : * the License at http://www.mozilla.org/MPL/
9 : : *
10 : : * Software distributed under the License is distributed on an "AS IS" basis,
11 : : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : : * for the specific language governing rights and limitations under the
13 : : * License.
14 : : *
15 : : * Major Contributor(s):
16 : : * Copyright (C) 2011 Red Hat, Inc., Stephan Bergmann <sbergman@redhat.com>
17 : : * (initial developer)
18 : : *
19 : : * All Rights Reserved.
20 : : *
21 : : * For minor contributions see the git repository.
22 : : *
23 : : * Alternatively, the contents of this file may be used under the terms of
24 : : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
25 : : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
26 : : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
27 : : * instead of those above.
28 : : */
29 : :
30 : : #include "sal/config.h"
31 : :
32 : : #include <cassert>
33 : :
34 : : #include "boost/shared_ptr.hpp"
35 : : #include "com/sun/star/configuration/ReadOnlyAccess.hpp"
36 : : #include "com/sun/star/configuration/ReadWriteAccess.hpp"
37 : : #include "com/sun/star/configuration/XReadWriteAccess.hpp"
38 : : #include "com/sun/star/configuration/theDefaultProvider.hpp"
39 : : #include "com/sun/star/container/XHierarchicalNameAccess.hpp"
40 : : #include "com/sun/star/container/XHierarchicalNameReplace.hpp"
41 : : #include "com/sun/star/container/XNameAccess.hpp"
42 : : #include "com/sun/star/container/XNameContainer.hpp"
43 : : #include "com/sun/star/lang/Locale.hpp"
44 : : #include "com/sun/star/lang/XLocalizable.hpp"
45 : : #include "com/sun/star/uno/Any.hxx"
46 : : #include "com/sun/star/uno/Reference.hxx"
47 : : #include "com/sun/star/uno/XComponentContext.hpp"
48 : : #include "comphelper/configuration.hxx"
49 : : #include "rtl/instance.hxx"
50 : : #include "rtl/oustringostreaminserter.hxx"
51 : : #include "rtl/ustrbuf.hxx"
52 : : #include "rtl/ustring.h"
53 : : #include "rtl/ustring.hxx"
54 : : #include "sal/log.hxx"
55 : :
56 : : namespace {
57 : :
58 : : namespace css = com::sun::star;
59 : :
60 : : struct TheConfigurationWrapper:
61 : : public rtl::StaticWithArg<
62 : : comphelper::detail::ConfigurationWrapper,
63 : : css::uno::Reference< css::uno::XComponentContext >,
64 : : TheConfigurationWrapper >
65 : : {};
66 : :
67 : 4814 : OUString getDefaultLocale(
68 : : css::uno::Reference< css::uno::XComponentContext > const & context)
69 : : {
70 : : css::lang::Locale locale(
71 : : css::uno::Reference< css::lang::XLocalizable >(
72 : : css::configuration::theDefaultProvider::get(context),
73 [ + - ][ + - ]: 9628 : css::uno::UNO_QUERY_THROW)->
[ + - ]
74 [ + - ]: 4814 : getLocale());
75 : 4814 : OUStringBuffer buf;
76 : : SAL_WARN_IF(
77 : : locale.Language.indexOf('-') != -1, "comphelper",
78 : : "Locale language \"" << locale.Language << "\" contains \"-\"");
79 [ + - ]: 4814 : buf.append(locale.Language);
80 : : SAL_WARN_IF(
81 : : locale.Country.isEmpty() && !locale.Variant.isEmpty(), "comphelper",
82 : : "Locale has empty country but non-empty variant \"" << locale.Variant
83 : : << '"');
84 [ + + ]: 4814 : if (!locale.Country.isEmpty()) {
85 [ + - ]: 2772 : buf.append('-');
86 : : SAL_WARN_IF(
87 : : locale.Country.indexOf('-') != -1, "comphelper",
88 : : "Locale language \"" << locale.Country << "\" contains \"-\"");
89 [ + - ]: 2772 : buf.append(locale.Country);
90 [ - + ]: 2772 : if (!locale.Variant.isEmpty()) {
91 [ # # ]: 0 : buf.append('-');
92 [ # # ]: 0 : buf.append(locale.Variant);
93 : : }
94 : : }
95 [ + - ]: 4814 : return buf.makeStringAndClear();
96 : : }
97 : :
98 : 0 : OUString extendLocalizedPath(OUString const & path, OUString const & locale) {
99 [ # # ]: 0 : rtl::OUStringBuffer buf(path);
100 [ # # ]: 0 : buf.append("/['*");
101 : : SAL_WARN_IF(
102 : : locale.match("*"), "comphelper",
103 : : "Locale \"" << locale << "\" starts with \"-\"");
104 : : assert(locale.indexOf('&') == -1);
105 : : assert(locale.indexOf('"') == -1);
106 : : assert(locale.indexOf('\'') == -1);
107 [ # # ]: 0 : buf.append(locale);
108 [ # # ]: 0 : buf.append("']");
109 [ # # ]: 0 : return buf.makeStringAndClear();
110 : : }
111 : :
112 : : }
113 : :
114 : : boost::shared_ptr< comphelper::ConfigurationChanges >
115 : 2359 : comphelper::ConfigurationChanges::create(
116 : : css::uno::Reference< css::uno::XComponentContext > const & context)
117 : : {
118 : 2359 : return TheConfigurationWrapper::get(context).createChanges();
119 : : }
120 : :
121 : :
122 : 2359 : comphelper::ConfigurationChanges::~ConfigurationChanges() {}
123 : :
124 : 62 : void comphelper::ConfigurationChanges::commit() const {
125 : 62 : access_->commitChanges();
126 : 62 : }
127 : :
128 : 2359 : comphelper::ConfigurationChanges::ConfigurationChanges(
129 : : css::uno::Reference< css::uno::XComponentContext > const & context):
130 : : access_(
131 : : css::configuration::ReadWriteAccess::create(
132 [ + - ][ + - ]: 2359 : context, getDefaultLocale(context)))
133 : 2359 : {}
134 : :
135 : 62 : void comphelper::ConfigurationChanges::setPropertyValue(
136 : : rtl::OUString const & path, css::uno::Any const & value) const
137 : : {
138 : 62 : access_->replaceByHierarchicalName(path, value);
139 : 62 : }
140 : :
141 : : css::uno::Reference< css::container::XHierarchicalNameReplace >
142 : 0 : comphelper::ConfigurationChanges::getGroup(rtl::OUString const & path) const
143 : : {
144 : : return css::uno::Reference< css::container::XHierarchicalNameReplace >(
145 [ # # ]: 0 : access_->getByHierarchicalName(path), css::uno::UNO_QUERY_THROW);
146 : : }
147 : :
148 : : css::uno::Reference< css::container::XNameContainer >
149 : 0 : comphelper::ConfigurationChanges::getSet(rtl::OUString const & path) const
150 : : {
151 : : return css::uno::Reference< css::container::XNameContainer >(
152 [ # # ]: 0 : access_->getByHierarchicalName(path), css::uno::UNO_QUERY_THROW);
153 : : }
154 : :
155 : : comphelper::detail::ConfigurationWrapper const &
156 : 26394 : comphelper::detail::ConfigurationWrapper::get(
157 : : css::uno::Reference< css::uno::XComponentContext > const & context)
158 : : {
159 : 26394 : return TheConfigurationWrapper::get(context);
160 : : }
161 : :
162 : 236 : comphelper::detail::ConfigurationWrapper::ConfigurationWrapper(
163 : : css::uno::Reference< css::uno::XComponentContext > const & context):
164 : : context_(context),
165 [ + - ]: 236 : access_(css::configuration::ReadOnlyAccess::create(context, "*"))
166 : 236 : {}
167 : :
168 : 236 : comphelper::detail::ConfigurationWrapper::~ConfigurationWrapper() {}
169 : :
170 : 23877 : css::uno::Any comphelper::detail::ConfigurationWrapper::getPropertyValue(
171 : : rtl::OUString const & path) const
172 : : {
173 : 23877 : return access_->getByHierarchicalName(path);
174 : : }
175 : :
176 : 62 : void comphelper::detail::ConfigurationWrapper::setPropertyValue(
177 : : boost::shared_ptr< ConfigurationChanges > const & batch,
178 : : rtl::OUString const & path, com::sun::star::uno::Any const & value) const
179 : : {
180 : : assert(batch.get() != 0);
181 : 62 : batch->setPropertyValue(path, value);
182 : 62 : }
183 : :
184 : : css::uno::Any
185 : 0 : comphelper::detail::ConfigurationWrapper::getLocalizedPropertyValue(
186 : : rtl::OUString const & path) const
187 : : {
188 : 0 : return access_->getByHierarchicalName(
189 [ # # ][ # # ]: 0 : extendLocalizedPath(path, getDefaultLocale(context_)));
190 : : }
191 : :
192 : 0 : void comphelper::detail::ConfigurationWrapper::setLocalizedPropertyValue(
193 : : boost::shared_ptr< ConfigurationChanges > const & batch,
194 : : rtl::OUString const & path, com::sun::star::uno::Any const & value) const
195 : : {
196 : : assert(batch.get() != 0);
197 : 0 : batch->setPropertyValue(path, value);
198 : 0 : }
199 : :
200 : : css::uno::Reference< css::container::XHierarchicalNameAccess >
201 : 0 : comphelper::detail::ConfigurationWrapper::getGroupReadOnly(
202 : : rtl::OUString const & path) const
203 : : {
204 : : return css::uno::Reference< css::container::XHierarchicalNameAccess >(
205 : : (css::configuration::ReadOnlyAccess::create(
206 [ # # ][ # # ]: 0 : context_, getDefaultLocale(context_))->
207 : 0 : getByHierarchicalName(path)),
208 [ # # ][ # # ]: 0 : css::uno::UNO_QUERY_THROW);
209 : : }
210 : :
211 : : css::uno::Reference< css::container::XHierarchicalNameReplace >
212 : 0 : comphelper::detail::ConfigurationWrapper::getGroupReadWrite(
213 : : boost::shared_ptr< ConfigurationChanges > const & batch,
214 : : rtl::OUString const & path) const
215 : : {
216 : : assert(batch.get() != 0);
217 : 0 : return batch->getGroup(path);
218 : : }
219 : :
220 : : css::uno::Reference< css::container::XNameAccess >
221 : 2455 : comphelper::detail::ConfigurationWrapper::getSetReadOnly(
222 : : rtl::OUString const & path) const
223 : : {
224 : : return css::uno::Reference< css::container::XNameAccess >(
225 : : (css::configuration::ReadOnlyAccess::create(
226 [ + - ][ + - ]: 4910 : context_, getDefaultLocale(context_))->
227 : 2455 : getByHierarchicalName(path)),
228 [ + - ][ + - ]: 4910 : css::uno::UNO_QUERY_THROW);
229 : : }
230 : :
231 : : css::uno::Reference< css::container::XNameContainer >
232 : 0 : comphelper::detail::ConfigurationWrapper::getSetReadWrite(
233 : : boost::shared_ptr< ConfigurationChanges > const & batch,
234 : : rtl::OUString const & path) const
235 : : {
236 : : assert(batch.get() != 0);
237 : 0 : return batch->getSet(path);
238 : : }
239 : :
240 : : boost::shared_ptr< comphelper::ConfigurationChanges >
241 : 2359 : comphelper::detail::ConfigurationWrapper::createChanges() const {
242 : : return boost::shared_ptr< ConfigurationChanges >(
243 [ + - ]: 2359 : new ConfigurationChanges(context_));
244 : : }
245 : :
246 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|