1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "sal/config.h"
25 #include "com/sun/star/container/XChild.hpp"
26 #include "com/sun/star/lang/NoSupportException.hpp"
27 #include "com/sun/star/lang/XUnoTunnel.hpp"
28 #include "com/sun/star/uno/Any.hxx"
29 #include "com/sun/star/uno/Reference.hxx"
30 #include "com/sun/star/uno/RuntimeException.hpp"
31 #include "com/sun/star/uno/Sequence.hxx"
32 #include "com/sun/star/uno/Type.hxx"
33 #include "com/sun/star/uno/XInterface.hpp"
34 #include "cppu/unotype.hxx"
35 #include "cppuhelper/queryinterface.hxx"
36 #include "cppuhelper/weak.hxx"
37 #include "comphelper/servicehelper.hxx"
38 #include "osl/mutex.hxx"
39 #include "rtl/ref.hxx"
40 #include "rtl/string.h"
41 #include "rtl/ustrbuf.hxx"
42 #include "rtl/ustring.h"
43 #include "rtl/ustring.hxx"
44 #include "sal/types.h"
47 #include "childaccess.hxx"
48 #include "components.hxx"
50 #include "groupnode.hxx"
51 #include "localizedpropertynode.hxx"
52 #include "localizedvaluenode.hxx"
54 #include "modifications.hxx"
57 #include "propertynode.hxx"
58 #include "rootaccess.hxx"
59 #include "setnode.hxx"
66 class theChildAccessUnoTunnelId
: public rtl::Static
< UnoTunnelIdInit
, theChildAccessUnoTunnelId
> {};
69 css::uno::Sequence
< sal_Int8
> ChildAccess::getTunnelId()
71 return theChildAccessUnoTunnelId::get().getSeq();
74 ChildAccess::ChildAccess(
75 Components
& components
, rtl::Reference
< RootAccess
> const & root
,
76 rtl::Reference
< Access
> const & parent
, rtl::OUString
const & name
,
77 rtl::Reference
< Node
> const & node
):
78 Access(components
), root_(root
), parent_(parent
), name_(name
), node_(node
),
82 assert(root
.is() && parent
.is() && node
.is());
85 ChildAccess::ChildAccess(
86 Components
& components
, rtl::Reference
< RootAccess
> const & root
,
87 rtl::Reference
< Node
> const & node
):
88 Access(components
), root_(root
), node_(node
), inTransaction_(false)
91 assert(root
.is() && node
.is());
94 Path
ChildAccess::getAbsolutePath() {
95 assert(getParentAccess().is());
96 Path
path(getParentAccess()->getAbsolutePath());
97 path
.push_back(name_
);
101 Path
ChildAccess::getRelativePath() {
103 rtl::Reference
< Access
> parent(getParentAccess());
105 path
= parent
->getRelativePath();
107 path
.push_back(name_
);
111 rtl::OUString
ChildAccess::getRelativePathRepresentation() {
112 rtl::OUStringBuffer path
;
113 rtl::Reference
< Access
> parent(getParentAccess());
115 path
.append(parent
->getRelativePathRepresentation());
116 if (path
.getLength() != 0) {
117 path
.append(sal_Unicode('/'));
120 path
.append(Data::createSegment(node_
->getTemplateName(), name_
));
121 return path
.makeStringAndClear();
124 rtl::Reference
< Node
> ChildAccess::getNode() {
128 bool ChildAccess::isFinalized() {
129 return node_
->getFinalized() != Data::NO_LAYER
||
130 (parent_
.is() && parent_
->isFinalized());
133 rtl::OUString
ChildAccess::getNameInternal() {
137 rtl::Reference
< RootAccess
> ChildAccess::getRootAccess() {
141 rtl::Reference
< Access
> ChildAccess::getParentAccess() {
145 void ChildAccess::acquire() throw () {
149 void ChildAccess::release() throw () {
153 css::uno::Reference
< css::uno::XInterface
> ChildAccess::getParent()
154 throw (css::uno::RuntimeException
)
156 assert(thisIs(IS_ANY
));
157 osl::MutexGuard
g(*lock_
);
158 checkLocalizedPropertyAccess();
159 return static_cast< cppu::OWeakObject
* >(parent_
.get());
162 void ChildAccess::setParent(css::uno::Reference
< css::uno::XInterface
> const &)
163 throw (css::lang::NoSupportException
, css::uno::RuntimeException
)
165 assert(thisIs(IS_ANY
));
166 osl::MutexGuard
g(*lock_
);
167 checkLocalizedPropertyAccess();
168 throw css::lang::NoSupportException(
169 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("setParent")),
170 static_cast< cppu::OWeakObject
* >(this));
173 sal_Int64
ChildAccess::getSomething(
174 css::uno::Sequence
< sal_Int8
> const & aIdentifier
)
175 throw (css::uno::RuntimeException
)
177 assert(thisIs(IS_ANY
));
178 osl::MutexGuard
g(*lock_
);
179 checkLocalizedPropertyAccess();
180 return aIdentifier
== getTunnelId()
181 ? reinterpret_cast< sal_Int64
>(this) : 0;
184 void ChildAccess::bind(
185 rtl::Reference
< RootAccess
> const & root
,
186 rtl::Reference
< Access
> const & parent
, rtl::OUString
const & name
)
189 assert(!parent_
.is() && root
.is() && parent
.is() && !name
.isEmpty());
195 void ChildAccess::unbind() throw () {
196 assert(parent_
.is());
197 parent_
->releaseChild(name_
);
199 inTransaction_
= true;
202 void ChildAccess::committed() {
203 inTransaction_
= false;
206 void ChildAccess::setNode(rtl::Reference
< Node
> const & node
) {
210 void ChildAccess::setProperty(
211 css::uno::Any
const & value
, Modifications
* localModifications
)
213 assert(localModifications
!= 0);
214 Type type
= TYPE_ERROR
;
215 bool nillable
= false;
216 switch (node_
->kind()) {
217 case Node::KIND_PROPERTY
:
219 PropertyNode
* prop
= dynamic_cast< PropertyNode
* >(node_
.get());
220 type
= prop
->getStaticType();
221 nillable
= prop
->isNillable();
224 case Node::KIND_LOCALIZED_PROPERTY
:
226 rtl::OUString
locale(getRootAccess()->getLocale());
227 if (!Components::allLocales(locale
)) {
228 rtl::Reference
< ChildAccess
> child(getChild(locale
));
230 child
->setProperty(value
, localModifications
);
232 insertLocalizedValueChild(
233 locale
, value
, localModifications
);
239 case Node::KIND_LOCALIZED_VALUE
:
241 LocalizedPropertyNode
* locprop
=
242 dynamic_cast< LocalizedPropertyNode
* >(getParentNode().get());
243 type
= locprop
->getStaticType();
244 nillable
= locprop
->isNillable();
250 checkValue(value
, type
, nillable
);
251 getParentAccess()->markChildAsModified(this);
252 changedValue_
.reset(new css::uno::Any(value
));
253 localModifications
->add(getRelativePath());
256 css::uno::Any
ChildAccess::asValue() {
257 if (changedValue_
.get() != 0) {
258 return *changedValue_
;
260 switch (node_
->kind()) {
261 case Node::KIND_PROPERTY
:
262 return dynamic_cast< PropertyNode
* >(node_
.get())->getValue(
264 case Node::KIND_LOCALIZED_PROPERTY
:
266 OUString
locale(getRootAccess()->getLocale());
267 if (!Components::allLocales(locale
)) {
268 rtl::Reference
< ChildAccess
> child(getChild("*" + locale
));
269 // As a last resort, return a nil value even though it may be
270 // illegal for the given property:
271 return child
.is() ? child
->asValue() : css::uno::Any();
275 case Node::KIND_LOCALIZED_VALUE
:
276 return dynamic_cast< LocalizedValueNode
* >(node_
.get())->getValue();
280 return css::uno::makeAny(
281 css::uno::Reference
< css::uno::XInterface
>(
282 static_cast< cppu::OWeakObject
* >(this)));
285 void ChildAccess::commitChanges(bool valid
, Modifications
* globalModifications
)
287 assert(globalModifications
!= 0);
288 commitChildChanges(valid
, globalModifications
);
289 if (valid
&& changedValue_
.get() != 0) {
290 Path
path(getAbsolutePath());
291 getComponents().addModification(path
);
292 globalModifications
->add(path
);
293 switch (node_
->kind()) {
294 case Node::KIND_PROPERTY
:
295 dynamic_cast< PropertyNode
* >(node_
.get())->setValue(
296 Data::NO_LAYER
, *changedValue_
);
298 case Node::KIND_LOCALIZED_VALUE
:
299 dynamic_cast< LocalizedValueNode
* >(node_
.get())->setValue(
300 Data::NO_LAYER
, *changedValue_
);
303 assert(false); // this cannot happen
307 changedValue_
.reset();
310 ChildAccess::~ChildAccess() {
311 osl::MutexGuard
g(*lock_
);
313 parent_
->releaseChild(name_
);
317 void ChildAccess::addTypes(std::vector
< css::uno::Type
> * types
) const {
319 types
->push_back(cppu::UnoType
< css::container::XChild
>::get());
320 types
->push_back(cppu::UnoType
< css::lang::XUnoTunnel
>::get());
323 void ChildAccess::addSupportedServiceNames(
324 std::vector
< rtl::OUString
> * services
)
326 assert(services
!= 0);
328 getParentNode()->kind() == Node::KIND_GROUP
330 RTL_CONSTASCII_USTRINGPARAM(
331 "com.sun.star.configuration.GroupElement"))
333 RTL_CONSTASCII_USTRINGPARAM(
334 "com.sun.star.configuration.SetElement")));
337 css::uno::Any
ChildAccess::queryInterface(css::uno::Type
const & aType
)
338 throw (css::uno::RuntimeException
)
340 assert(thisIs(IS_ANY
));
341 osl::MutexGuard
g(*lock_
);
342 checkLocalizedPropertyAccess();
343 css::uno::Any
res(Access::queryInterface(aType
));
344 return res
.hasValue()
346 : cppu::queryInterface(
347 aType
, static_cast< css::container::XChild
* >(this),
348 static_cast< css::lang::XUnoTunnel
* >(this));
353 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */