1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 #include "precompiled_configmgr.hxx"
25 #include "sal/config.h"
26
27 #include <vector>
28
29 #include "com/sun/star/container/XChild.hpp"
30 #include "com/sun/star/lang/NoSupportException.hpp"
31 #include "com/sun/star/lang/XUnoTunnel.hpp"
32 #include "com/sun/star/uno/Any.hxx"
33 #include "com/sun/star/uno/Reference.hxx"
34 #include "com/sun/star/uno/RuntimeException.hpp"
35 #include "com/sun/star/uno/Sequence.hxx"
36 #include "com/sun/star/uno/Type.hxx"
37 #include "com/sun/star/uno/XInterface.hpp"
38 #include "cppu/unotype.hxx"
39 #include "cppuhelper/queryinterface.hxx"
40 #include "cppuhelper/weak.hxx"
41 #include "osl/diagnose.h"
42 #include "osl/mutex.hxx"
43 #include "rtl/ref.hxx"
44 #include "rtl/string.h"
45 #include "rtl/ustrbuf.hxx"
46 #include "rtl/ustring.h"
47 #include "rtl/ustring.hxx"
48 #include "rtl/uuid.h"
49 #include "sal/types.h"
50
51 #include "access.hxx"
52 #include "childaccess.hxx"
53 #include "components.hxx"
54 #include "data.hxx"
55 #include "groupnode.hxx"
56 #include "localizedpropertynode.hxx"
57 #include "localizedvaluenode.hxx"
58 #include "lock.hxx"
59 #include "modifications.hxx"
60 #include "node.hxx"
61 #include "path.hxx"
62 #include "propertynode.hxx"
63 #include "rootaccess.hxx"
64 #include "setnode.hxx"
65 #include "type.hxx"
66
67 namespace configmgr {
68
69 namespace {
70
71 namespace css = com::sun::star;
72
73 }
74
getTunnelId()75 css::uno::Sequence< sal_Int8 > ChildAccess::getTunnelId() {
76 static css::uno::Sequence< sal_Int8 > id;
77 if (id.getLength() == 0) {
78 css::uno::Sequence< sal_Int8 > uuid(16);
79 rtl_createUuid(
80 reinterpret_cast< sal_uInt8 * >(uuid.getArray()), 0, false);
81 id = uuid;
82 }
83 return id;
84 }
85
ChildAccess(Components & components,rtl::Reference<RootAccess> const & root,rtl::Reference<Access> const & parent,rtl::OUString const & name,rtl::Reference<Node> const & node)86 ChildAccess::ChildAccess(
87 Components & components, rtl::Reference< RootAccess > const & root,
88 rtl::Reference< Access > const & parent, rtl::OUString const & name,
89 rtl::Reference< Node > const & node):
90 Access(components), root_(root), parent_(parent), name_(name), node_(node),
91 inTransaction_(false)
92 {
93 OSL_ASSERT(root.is() && parent.is() && node.is());
94 }
95
ChildAccess(Components & components,rtl::Reference<RootAccess> const & root,rtl::Reference<Node> const & node)96 ChildAccess::ChildAccess(
97 Components & components, rtl::Reference< RootAccess > const & root,
98 rtl::Reference< Node > const & node):
99 Access(components), root_(root), node_(node), inTransaction_(false)
100 {
101 OSL_ASSERT(root.is() && node.is());
102 }
103
getAbsolutePath()104 Path ChildAccess::getAbsolutePath() {
105 OSL_ASSERT(getParentAccess().is());
106 Path path(getParentAccess()->getAbsolutePath());
107 path.push_back(name_);
108 return path;
109 }
110
getRelativePath()111 Path ChildAccess::getRelativePath() {
112 Path path;
113 rtl::Reference< Access > parent(getParentAccess());
114 if (parent.is()) {
115 path = parent->getRelativePath();
116 }
117 path.push_back(name_);
118 return path;
119 }
120
getRelativePathRepresentation()121 rtl::OUString ChildAccess::getRelativePathRepresentation() {
122 rtl::OUStringBuffer path;
123 rtl::Reference< Access > parent(getParentAccess());
124 if (parent.is()) {
125 path.append(parent->getRelativePathRepresentation());
126 if (path.getLength() != 0) {
127 path.append(sal_Unicode('/'));
128 }
129 }
130 path.append(Data::createSegment(node_->getTemplateName(), name_));
131 return path.makeStringAndClear();
132 }
133
getNode()134 rtl::Reference< Node > ChildAccess::getNode() {
135 return node_;
136 }
137
isFinalized()138 bool ChildAccess::isFinalized() {
139 return node_->getFinalized() != Data::NO_LAYER ||
140 (parent_.is() && parent_->isFinalized());
141 }
142
getNameInternal()143 rtl::OUString ChildAccess::getNameInternal() {
144 return name_;
145 }
146
getRootAccess()147 rtl::Reference< RootAccess > ChildAccess::getRootAccess() {
148 return root_;
149 }
150
getParentAccess()151 rtl::Reference< Access > ChildAccess::getParentAccess() {
152 return parent_;
153 }
154
acquire()155 void ChildAccess::acquire() throw () {
156 Access::acquire();
157 }
158
release()159 void ChildAccess::release() throw () {
160 Access::release();
161 }
162
getParent()163 css::uno::Reference< css::uno::XInterface > ChildAccess::getParent()
164 {
165 OSL_ASSERT(thisIs(IS_ANY));
166 osl::MutexGuard g(lock);
167 checkLocalizedPropertyAccess();
168 return static_cast< cppu::OWeakObject * >(parent_.get());
169 }
170
setParent(css::uno::Reference<css::uno::XInterface> const &)171 void ChildAccess::setParent(css::uno::Reference< css::uno::XInterface > const &)
172 {
173 OSL_ASSERT(thisIs(IS_ANY));
174 osl::MutexGuard g(lock);
175 checkLocalizedPropertyAccess();
176 throw css::lang::NoSupportException(
177 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("setParent")),
178 static_cast< cppu::OWeakObject * >(this));
179 }
180
getSomething(css::uno::Sequence<sal_Int8> const & aIdentifier)181 sal_Int64 ChildAccess::getSomething(
182 css::uno::Sequence< sal_Int8 > const & aIdentifier)
183 {
184 OSL_ASSERT(thisIs(IS_ANY));
185 osl::MutexGuard g(lock);
186 checkLocalizedPropertyAccess();
187 return aIdentifier == getTunnelId()
188 ? reinterpret_cast< sal_Int64 >(this) : 0;
189 }
190
bind(rtl::Reference<RootAccess> const & root,rtl::Reference<Access> const & parent,rtl::OUString const & name)191 void ChildAccess::bind(
192 rtl::Reference< RootAccess > const & root,
193 rtl::Reference< Access > const & parent, rtl::OUString const & name)
194 throw ()
195 {
196 OSL_ASSERT(
197 !parent_.is() && root.is() && parent.is() && name.getLength() != 0);
198 root_ = root;
199 parent_ = parent;
200 name_ = name;
201 }
202
unbind()203 void ChildAccess::unbind() throw () {
204 OSL_ASSERT(parent_.is());
205 parent_->releaseChild(name_);
206 parent_.clear();
207 inTransaction_ = true;
208 }
209
committed()210 void ChildAccess::committed() {
211 inTransaction_ = false;
212 }
213
setNode(rtl::Reference<Node> const & node)214 void ChildAccess::setNode(rtl::Reference< Node > const & node) {
215 node_ = node;
216 }
217
setProperty(css::uno::Any const & value,Modifications * localModifications)218 void ChildAccess::setProperty(
219 css::uno::Any const & value, Modifications * localModifications)
220 {
221 OSL_ASSERT(localModifications != 0);
222 Type type = TYPE_ERROR;
223 bool nillable = false;
224 switch (node_->kind()) {
225 case Node::KIND_PROPERTY:
226 {
227 PropertyNode * prop = dynamic_cast< PropertyNode * >(node_.get());
228 type = prop->getStaticType();
229 nillable = prop->isNillable();
230 }
231 break;
232 case Node::KIND_LOCALIZED_PROPERTY:
233 {
234 rtl::OUString locale(getRootAccess()->getLocale());
235 if (!Components::allLocales(locale)) {
236 rtl::Reference< ChildAccess > child(getChild(locale));
237 if (child.is()) {
238 child->setProperty(value, localModifications);
239 } else {
240 insertLocalizedValueChild(
241 locale, value, localModifications);
242 }
243 return;
244 }
245 }
246 break;
247 case Node::KIND_LOCALIZED_VALUE:
248 {
249 LocalizedPropertyNode * locprop =
250 dynamic_cast< LocalizedPropertyNode * >(getParentNode().get());
251 type = locprop->getStaticType();
252 nillable = locprop->isNillable();
253 }
254 break;
255 default:
256 break;
257 }
258 checkValue(value, type, nillable);
259 getParentAccess()->markChildAsModified(this);
260 changedValue_.reset(new css::uno::Any(value));
261 localModifications->add(getRelativePath());
262 }
263
asValue()264 css::uno::Any ChildAccess::asValue() {
265 if (changedValue_.get() != 0) {
266 return *changedValue_;
267 }
268 switch (node_->kind()) {
269 case Node::KIND_PROPERTY:
270 return dynamic_cast< PropertyNode * >(node_.get())->getValue(
271 getComponents());
272 case Node::KIND_LOCALIZED_PROPERTY:
273 {
274 rtl::OUString locale(getRootAccess()->getLocale());
275 if (!Components::allLocales(locale)) {
276 // Find best match using an adaption of RFC 4647 lookup matching
277 // rules, removing "-" or "_" delimited segments from the end;
278 // defaults are the "en-US" locale, the "en" locale, the empty
279 // string locale, the first child (if any), or a nil value (even
280 // though it may be illegal for the given property), in that
281 // order:
282 rtl::Reference< ChildAccess > child;
283 for (;;) {
284 child = getChild(locale);
285 if (child.is() || locale.getLength() == 0) {
286 break;
287 }
288 sal_Int32 i = locale.getLength() - 1;
289 while (i > 0 && locale[i] != '-' && locale[i] != '_') {
290 --i;
291 }
292 if (i == 0) {
293 break;
294 }
295 locale = locale.copy(0, i);
296 }
297 if (!child.is()) {
298 child = getChild(
299 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en-US")));
300 if (!child.is()) {
301 child = getChild(
302 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en")));
303 if (!child.is()) {
304 child = getChild(rtl::OUString());
305 if (!child.is()) {
306 std::vector< rtl::Reference< ChildAccess > >
307 all(getAllChildren());
308 if (!all.empty()) {
309 child = all.front();
310 }
311 }
312 }
313 }
314 }
315 return child.is() ? child->asValue() : css::uno::Any();
316 }
317 }
318 break;
319 case Node::KIND_LOCALIZED_VALUE:
320 return dynamic_cast< LocalizedValueNode * >(node_.get())->getValue();
321 default:
322 break;
323 }
324 return css::uno::makeAny(
325 css::uno::Reference< css::uno::XInterface >(
326 static_cast< cppu::OWeakObject * >(this)));
327 }
328
commitChanges(bool valid,Modifications * globalModifications)329 void ChildAccess::commitChanges(bool valid, Modifications * globalModifications)
330 {
331 OSL_ASSERT(globalModifications != 0);
332 commitChildChanges(valid, globalModifications);
333 if (valid && changedValue_.get() != 0) {
334 Path path(getAbsolutePath());
335 getComponents().addModification(path);
336 globalModifications->add(path);
337 switch (node_->kind()) {
338 case Node::KIND_PROPERTY:
339 dynamic_cast< PropertyNode * >(node_.get())->setValue(
340 Data::NO_LAYER, *changedValue_);
341 break;
342 case Node::KIND_LOCALIZED_VALUE:
343 dynamic_cast< LocalizedValueNode * >(node_.get())->setValue(
344 Data::NO_LAYER, *changedValue_);
345 break;
346 default:
347 OSL_ASSERT(false); // this cannot happen
348 break;
349 }
350 }
351 changedValue_.reset();
352 }
353
~ChildAccess()354 ChildAccess::~ChildAccess() {
355 osl::MutexGuard g(lock);
356 if (parent_.is()) {
357 parent_->releaseChild(name_);
358 }
359 }
360
addTypes(std::vector<css::uno::Type> * types) const361 void ChildAccess::addTypes(std::vector< css::uno::Type > * types) const {
362 OSL_ASSERT(types != 0);
363 types->push_back(cppu::UnoType< css::container::XChild >::get());
364 types->push_back(cppu::UnoType< css::lang::XUnoTunnel >::get());
365 }
366
addSupportedServiceNames(std::vector<rtl::OUString> * services)367 void ChildAccess::addSupportedServiceNames(
368 std::vector< rtl::OUString > * services)
369 {
370 OSL_ASSERT(services != 0);
371 services->push_back(
372 getParentNode()->kind() == Node::KIND_GROUP
373 ? rtl::OUString(
374 RTL_CONSTASCII_USTRINGPARAM(
375 "com.sun.star.configuration.GroupElement"))
376 : rtl::OUString(
377 RTL_CONSTASCII_USTRINGPARAM(
378 "com.sun.star.configuration.SetElement")));
379 }
380
queryInterface(css::uno::Type const & aType)381 css::uno::Any ChildAccess::queryInterface(css::uno::Type const & aType)
382 {
383 OSL_ASSERT(thisIs(IS_ANY));
384 osl::MutexGuard g(lock);
385 checkLocalizedPropertyAccess();
386 css::uno::Any res(Access::queryInterface(aType));
387 return res.hasValue()
388 ? res
389 : cppu::queryInterface(
390 aType, static_cast< css::container::XChild * >(this),
391 static_cast< css::lang::XUnoTunnel * >(this));
392 }
393
394 }
395