bump product version to 7.2.5.1
[LibreOffice.git] / configmgr / source / childaccess.cxx
blob41636b0e1bbe9a0d18e347536d8cb19ef6a50a7e
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/.
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>
22 #include <cassert>
23 #include <vector>
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/Sequence.hxx>
31 #include <com/sun/star/uno/Type.hxx>
32 #include <com/sun/star/uno/XInterface.hpp>
33 #include <cppu/unotype.hxx>
34 #include <cppuhelper/queryinterface.hxx>
35 #include <cppuhelper/weak.hxx>
36 #include <comphelper/servicehelper.hxx>
37 #include <osl/mutex.hxx>
38 #include <rtl/ref.hxx>
39 #include <rtl/ustrbuf.hxx>
40 #include <rtl/ustring.hxx>
41 #include <sal/types.h>
43 #include "access.hxx"
44 #include "childaccess.hxx"
45 #include "components.hxx"
46 #include "data.hxx"
47 #include "localizedpropertynode.hxx"
48 #include "localizedvaluenode.hxx"
49 #include "lock.hxx"
50 #include "modifications.hxx"
51 #include "node.hxx"
52 #include "propertynode.hxx"
53 #include "rootaccess.hxx"
54 #include "type.hxx"
56 namespace configmgr {
58 css::uno::Sequence< sal_Int8 > const & ChildAccess::getTunnelId()
60 static const UnoTunnelIdInit theChildAccessUnoTunnelId;
61 return theChildAccessUnoTunnelId.getSeq();
64 ChildAccess::ChildAccess(
65 Components & components, rtl::Reference< RootAccess > const & root,
66 rtl::Reference< Access > const & parent, OUString const & name,
67 rtl::Reference< Node > const & node):
68 Access(components), root_(root), parent_(parent), name_(name), node_(node),
69 inTransaction_(false),
70 lock_( lock() )
72 assert(root.is() && parent.is() && node.is());
75 ChildAccess::ChildAccess(
76 Components & components, rtl::Reference< RootAccess > const & root,
77 rtl::Reference< Node > const & node):
78 Access(components), root_(root), node_(node), inTransaction_(false),
79 lock_( lock() )
81 assert(root.is() && node.is());
84 std::vector<OUString> ChildAccess::getAbsolutePath() {
85 rtl::Reference< Access > parent(getParentAccess());
86 assert(parent.is());
87 std::vector<OUString> path(parent->getAbsolutePath());
88 path.push_back(name_);
89 return path;
92 std::vector<OUString> ChildAccess::getRelativePath() {
93 std::vector<OUString> path;
94 rtl::Reference< Access > parent(getParentAccess());
95 if (parent.is()) {
96 path = parent->getRelativePath();
98 path.push_back(name_);
99 return path;
102 OUString ChildAccess::getRelativePathRepresentation() {
103 OUStringBuffer path(128);
104 rtl::Reference< Access > parent(getParentAccess());
105 if (parent.is()) {
106 path.append(parent->getRelativePathRepresentation());
107 if (!path.isEmpty()) {
108 path.append('/');
111 path.append(Data::createSegment(node_->getTemplateName(), name_));
112 return path.makeStringAndClear();
115 rtl::Reference< Node > ChildAccess::getNode() {
116 return node_;
119 bool ChildAccess::isFinalized() {
120 return node_->getFinalized() != Data::NO_LAYER ||
121 (parent_.is() && parent_->isFinalized());
124 OUString ChildAccess::getNameInternal() {
125 return name_;
128 rtl::Reference< RootAccess > ChildAccess::getRootAccess() {
129 return root_;
132 rtl::Reference< Access > ChildAccess::getParentAccess() {
133 return parent_;
136 void ChildAccess::acquire() noexcept {
137 Access::acquire();
140 void ChildAccess::release() noexcept {
141 Access::release();
144 css::uno::Reference< css::uno::XInterface > ChildAccess::getParent()
146 assert(thisIs(IS_ANY));
147 osl::MutexGuard g(*lock_);
148 checkLocalizedPropertyAccess();
149 return static_cast< cppu::OWeakObject * >(parent_.get());
152 void ChildAccess::setParent(css::uno::Reference< css::uno::XInterface > const &)
154 assert(thisIs(IS_ANY));
155 osl::MutexGuard g(*lock_);
156 checkLocalizedPropertyAccess();
157 throw css::lang::NoSupportException(
158 "setParent", static_cast< cppu::OWeakObject * >(this));
161 sal_Int64 ChildAccess::getSomething(
162 css::uno::Sequence< sal_Int8 > const & aIdentifier)
164 assert(thisIs(IS_ANY));
165 osl::MutexGuard g(*lock_);
166 checkLocalizedPropertyAccess();
167 return aIdentifier == getTunnelId()
168 ? reinterpret_cast< sal_Int64 >(this) : 0;
171 void ChildAccess::bind(
172 rtl::Reference< RootAccess > const & root,
173 rtl::Reference< Access > const & parent, OUString const & name)
174 noexcept
176 assert(!parent_.is() && root.is() && parent.is() && !name.isEmpty());
177 root_ = root;
178 parent_ = parent;
179 name_ = name;
182 void ChildAccess::unbind() noexcept {
183 assert(parent_.is());
184 parent_->releaseChild(name_);
185 parent_.clear();
186 inTransaction_ = true;
189 void ChildAccess::committed() {
190 inTransaction_ = false;
193 void ChildAccess::setNode(rtl::Reference< Node > const & node) {
194 node_ = node;
197 void ChildAccess::setProperty(
198 css::uno::Any const & value, Modifications * localModifications)
200 assert(localModifications != nullptr);
201 Type type = TYPE_ERROR;
202 bool isNillable = false;
203 switch (node_->kind()) {
204 case Node::KIND_PROPERTY:
206 PropertyNode * prop = static_cast< PropertyNode * >(node_.get());
207 type = prop->getStaticType();
208 isNillable = prop->isNillable();
210 break;
211 case Node::KIND_LOCALIZED_PROPERTY:
213 OUString locale(getRootAccess()->getLocale());
214 if (!Components::allLocales(locale)) {
215 rtl::Reference< ChildAccess > child(getChild(locale));
216 if (child.is()) {
217 child->setProperty(value, localModifications);
218 } else {
219 insertLocalizedValueChild(
220 locale, value, localModifications);
222 return;
225 break;
226 case Node::KIND_LOCALIZED_VALUE:
228 LocalizedPropertyNode * locprop =
229 static_cast< LocalizedPropertyNode * >(getParentNode().get());
230 type = locprop->getStaticType();
231 isNillable = locprop->isNillable();
233 break;
234 default:
235 break;
237 checkValue(value, type, isNillable);
238 getParentAccess()->markChildAsModified(this);
239 changedValue_.reset(new css::uno::Any(value));
240 localModifications->add(getRelativePath());
244 css::uno::Any ChildAccess::asValue()
246 if (changedValue_ != nullptr)
248 return *changedValue_;
250 css::uno::Any value;
251 if (!asSimpleValue(node_, value, getComponents()))
253 if (node_->kind() == Node::KIND_LOCALIZED_PROPERTY)
255 OUString locale(getRootAccess()->getLocale());
256 if (!Components::allLocales(locale)) {
257 rtl::Reference< ChildAccess > child(getChild("*" + locale));
258 // As a last resort, return a nil value even though it may be
259 // illegal for the given property:
260 return child.is() ? child->asValue() : css::uno::Any();
263 value <<= css::uno::Reference< css::uno::XInterface >(
264 static_cast< cppu::OWeakObject * >(this));
266 return value;
269 /// Can we quickly extract a simple value into value ? if so returns true
270 bool ChildAccess::asSimpleValue(const rtl::Reference< Node > &rNode,
271 css::uno::Any &value,
272 Components &components)
274 switch (rNode->kind()) {
275 case Node::KIND_PROPERTY:
276 value = static_cast< PropertyNode * >(rNode.get())->getValue(components);
277 return true;
278 case Node::KIND_LOCALIZED_VALUE:
279 value = static_cast< LocalizedValueNode * >(rNode.get())->getValue();
280 return true;
281 default:
282 return false;
286 void ChildAccess::commitChanges(bool valid, Modifications * globalModifications)
288 assert(globalModifications != nullptr);
289 commitChildChanges(valid, globalModifications);
290 if (valid && changedValue_ != nullptr)
292 std::vector<OUString> path(getAbsolutePath());
293 getComponents().addModification(path);
294 globalModifications->add(path);
295 switch (node_->kind()) {
296 case Node::KIND_PROPERTY:
297 static_cast< PropertyNode * >(node_.get())->setValue(
298 Data::NO_LAYER, *changedValue_);
299 break;
300 case Node::KIND_LOCALIZED_VALUE:
301 static_cast< LocalizedValueNode * >(node_.get())->setValue(
302 Data::NO_LAYER, *changedValue_);
303 break;
304 default:
305 assert(false); // this cannot happen
306 break;
309 changedValue_.reset();
312 ChildAccess::~ChildAccess() {
313 osl::MutexGuard g(*lock_);
314 if (parent_.is()) {
315 parent_->releaseChild(name_);
319 void ChildAccess::addTypes(std::vector< css::uno::Type > * types) const {
320 assert(types != nullptr);
321 types->push_back(cppu::UnoType< css::container::XChild >::get());
322 types->push_back(cppu::UnoType< css::lang::XUnoTunnel >::get());
325 void ChildAccess::addSupportedServiceNames(
326 std::vector<OUString> * services)
328 assert(services != nullptr);
329 services->push_back(
330 getParentNode()->kind() == Node::KIND_GROUP
331 ? OUString("com.sun.star.configuration.GroupElement")
332 : OUString("com.sun.star.configuration.SetElement"));
335 css::uno::Any ChildAccess::queryInterface(css::uno::Type const & aType)
337 assert(thisIs(IS_ANY));
338 osl::MutexGuard g(*lock_);
339 checkLocalizedPropertyAccess();
340 css::uno::Any res(Access::queryInterface(aType));
341 return res.hasValue()
342 ? res
343 : cppu::queryInterface(
344 aType, static_cast< css::container::XChild * >(this),
345 static_cast< css::lang::XUnoTunnel * >(this));
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */