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/ustrbuf.hxx"
51 : #include "rtl/ustring.hxx"
52 :
53 : namespace {
54 :
55 : struct TheConfigurationWrapper:
56 : public rtl::StaticWithArg<
57 : comphelper::detail::ConfigurationWrapper,
58 : css::uno::Reference< css::uno::XComponentContext >,
59 : TheConfigurationWrapper >
60 : {};
61 :
62 687 : OUString getDefaultLocale(
63 : css::uno::Reference< css::uno::XComponentContext > const & context)
64 : {
65 : css::lang::Locale locale(
66 : css::uno::Reference< css::lang::XLocalizable >(
67 : css::configuration::theDefaultProvider::get(context),
68 1374 : css::uno::UNO_QUERY_THROW)->
69 687 : getLocale());
70 687 : OUStringBuffer buf;
71 : SAL_WARN_IF(
72 : locale.Language.indexOf('-') != -1, "comphelper",
73 : "Locale language \"" << locale.Language << "\" contains \"-\"");
74 687 : buf.append(locale.Language);
75 : SAL_WARN_IF(
76 : locale.Country.isEmpty() && !locale.Variant.isEmpty(), "comphelper",
77 : "Locale has empty country but non-empty variant \"" << locale.Variant
78 : << '"');
79 687 : if (!locale.Country.isEmpty()) {
80 0 : buf.append('-');
81 : SAL_WARN_IF(
82 : locale.Country.indexOf('-') != -1, "comphelper",
83 : "Locale language \"" << locale.Country << "\" contains \"-\"");
84 0 : buf.append(locale.Country);
85 0 : if (!locale.Variant.isEmpty()) {
86 0 : buf.append('-');
87 0 : buf.append(locale.Variant);
88 : }
89 : }
90 687 : return buf.makeStringAndClear();
91 : }
92 :
93 0 : OUString extendLocalizedPath(OUString const & path, OUString const & locale) {
94 0 : rtl::OUStringBuffer buf(path);
95 0 : buf.append("/['*");
96 : SAL_WARN_IF(
97 : locale.match("*"), "comphelper",
98 : "Locale \"" << locale << "\" starts with \"-\"");
99 : assert(locale.indexOf('&') == -1);
100 : assert(locale.indexOf('"') == -1);
101 : assert(locale.indexOf('\'') == -1);
102 0 : buf.append(locale);
103 0 : buf.append("']");
104 0 : return buf.makeStringAndClear();
105 : }
106 :
107 : }
108 :
109 : boost::shared_ptr< comphelper::ConfigurationChanges >
110 342 : comphelper::ConfigurationChanges::create(
111 : css::uno::Reference< css::uno::XComponentContext > const & context)
112 : {
113 342 : return TheConfigurationWrapper::get(context).createChanges();
114 : }
115 :
116 :
117 342 : comphelper::ConfigurationChanges::~ConfigurationChanges() {}
118 :
119 0 : void comphelper::ConfigurationChanges::commit() const {
120 0 : access_->commitChanges();
121 0 : }
122 :
123 342 : comphelper::ConfigurationChanges::ConfigurationChanges(
124 : css::uno::Reference< css::uno::XComponentContext > const & context):
125 : access_(
126 : css::configuration::ReadWriteAccess::create(
127 342 : context, getDefaultLocale(context)))
128 342 : {}
129 :
130 0 : void comphelper::ConfigurationChanges::setPropertyValue(
131 : rtl::OUString const & path, css::uno::Any const & value) const
132 : {
133 0 : access_->replaceByHierarchicalName(path, value);
134 0 : }
135 :
136 : css::uno::Reference< css::container::XHierarchicalNameReplace >
137 0 : comphelper::ConfigurationChanges::getGroup(rtl::OUString const & path) const
138 : {
139 : return css::uno::Reference< css::container::XHierarchicalNameReplace >(
140 0 : access_->getByHierarchicalName(path), css::uno::UNO_QUERY_THROW);
141 : }
142 :
143 : css::uno::Reference< css::container::XNameContainer >
144 0 : comphelper::ConfigurationChanges::getSet(rtl::OUString const & path) const
145 : {
146 : return css::uno::Reference< css::container::XNameContainer >(
147 0 : access_->getByHierarchicalName(path), css::uno::UNO_QUERY_THROW);
148 : }
149 :
150 : comphelper::detail::ConfigurationWrapper const &
151 4381 : comphelper::detail::ConfigurationWrapper::get(
152 : css::uno::Reference< css::uno::XComponentContext > const & context)
153 : {
154 4381 : return TheConfigurationWrapper::get(context);
155 : }
156 :
157 20 : comphelper::detail::ConfigurationWrapper::ConfigurationWrapper(
158 : css::uno::Reference< css::uno::XComponentContext > const & context):
159 : context_(context),
160 20 : access_(css::configuration::ReadOnlyAccess::create(context, "*"))
161 20 : {}
162 :
163 20 : comphelper::detail::ConfigurationWrapper::~ConfigurationWrapper() {}
164 :
165 4036 : css::uno::Any comphelper::detail::ConfigurationWrapper::getPropertyValue(
166 : rtl::OUString const & path) const
167 : {
168 4036 : return access_->getByHierarchicalName(path);
169 : }
170 :
171 0 : void comphelper::detail::ConfigurationWrapper::setPropertyValue(
172 : boost::shared_ptr< ConfigurationChanges > const & batch,
173 : rtl::OUString const & path, com::sun::star::uno::Any const & value) const
174 : {
175 : assert(batch.get() != 0);
176 0 : batch->setPropertyValue(path, value);
177 0 : }
178 :
179 : css::uno::Any
180 0 : comphelper::detail::ConfigurationWrapper::getLocalizedPropertyValue(
181 : rtl::OUString const & path) const
182 : {
183 0 : return access_->getByHierarchicalName(
184 0 : extendLocalizedPath(path, getDefaultLocale(context_)));
185 : }
186 :
187 0 : void comphelper::detail::ConfigurationWrapper::setLocalizedPropertyValue(
188 : boost::shared_ptr< ConfigurationChanges > const & batch,
189 : rtl::OUString const & path, com::sun::star::uno::Any const & value) const
190 : {
191 : assert(batch.get() != 0);
192 0 : batch->setPropertyValue(path, value);
193 0 : }
194 :
195 : css::uno::Reference< css::container::XHierarchicalNameAccess >
196 3 : comphelper::detail::ConfigurationWrapper::getGroupReadOnly(
197 : rtl::OUString const & path) const
198 : {
199 : return css::uno::Reference< css::container::XHierarchicalNameAccess >(
200 : (css::configuration::ReadOnlyAccess::create(
201 6 : context_, getDefaultLocale(context_))->
202 3 : getByHierarchicalName(path)),
203 6 : css::uno::UNO_QUERY_THROW);
204 : }
205 :
206 : css::uno::Reference< css::container::XHierarchicalNameReplace >
207 0 : comphelper::detail::ConfigurationWrapper::getGroupReadWrite(
208 : boost::shared_ptr< ConfigurationChanges > const & batch,
209 : rtl::OUString const & path) const
210 : {
211 : assert(batch.get() != 0);
212 0 : return batch->getGroup(path);
213 : }
214 :
215 : css::uno::Reference< css::container::XNameAccess >
216 342 : comphelper::detail::ConfigurationWrapper::getSetReadOnly(
217 : rtl::OUString const & path) const
218 : {
219 : return css::uno::Reference< css::container::XNameAccess >(
220 : (css::configuration::ReadOnlyAccess::create(
221 684 : context_, getDefaultLocale(context_))->
222 342 : getByHierarchicalName(path)),
223 684 : css::uno::UNO_QUERY_THROW);
224 : }
225 :
226 : css::uno::Reference< css::container::XNameContainer >
227 0 : comphelper::detail::ConfigurationWrapper::getSetReadWrite(
228 : boost::shared_ptr< ConfigurationChanges > const & batch,
229 : rtl::OUString const & path) const
230 : {
231 : assert(batch.get() != 0);
232 0 : return batch->getSet(path);
233 : }
234 :
235 : boost::shared_ptr< comphelper::ConfigurationChanges >
236 342 : comphelper::detail::ConfigurationWrapper::createChanges() const {
237 : return boost::shared_ptr< ConfigurationChanges >(
238 342 : new ConfigurationChanges(context_));
239 : }
240 :
241 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|