build fix
[LibreOffice.git] / configmgr / source / childaccess.cxx
blob2cde81159b4adfe2e19f5922e861cfc08e7fccce
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/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>
46 #include "access.hxx"
47 #include "childaccess.hxx"
48 #include "components.hxx"
49 #include "data.hxx"
50 #include "groupnode.hxx"
51 #include "localizedpropertynode.hxx"
52 #include "localizedvaluenode.hxx"
53 #include "lock.hxx"
54 #include "modifications.hxx"
55 #include "node.hxx"
56 #include "propertynode.hxx"
57 #include "rootaccess.hxx"
58 #include "setnode.hxx"
59 #include "type.hxx"
61 namespace configmgr {
63 namespace
65 class theChildAccessUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theChildAccessUnoTunnelId > {};
68 css::uno::Sequence< sal_Int8 > ChildAccess::getTunnelId()
70 return theChildAccessUnoTunnelId::get().getSeq();
73 ChildAccess::ChildAccess(
74 Components & components, rtl::Reference< RootAccess > const & root,
75 rtl::Reference< Access > const & parent, OUString const & name,
76 rtl::Reference< Node > const & node):
77 Access(components), root_(root), parent_(parent), name_(name), node_(node),
78 inTransaction_(false),
79 lock_( lock() )
81 assert(root.is() && parent.is() && node.is());
84 ChildAccess::ChildAccess(
85 Components & components, rtl::Reference< RootAccess > const & root,
86 rtl::Reference< Node > const & node):
87 Access(components), root_(root), node_(node), inTransaction_(false),
88 lock_( lock() )
90 assert(root.is() && node.is());
93 std::vector<OUString> ChildAccess::getAbsolutePath() {
94 rtl::Reference< Access > parent(getParentAccess());
95 assert(parent.is());
96 std::vector<OUString> path(parent->getAbsolutePath());
97 path.push_back(name_);
98 return path;
101 std::vector<OUString> ChildAccess::getRelativePath() {
102 std::vector<OUString> path;
103 rtl::Reference< Access > parent(getParentAccess());
104 if (parent.is()) {
105 path = parent->getRelativePath();
107 path.push_back(name_);
108 return path;
111 OUString ChildAccess::getRelativePathRepresentation() {
112 OUStringBuffer path;
113 rtl::Reference< Access > parent(getParentAccess());
114 if (parent.is()) {
115 path.append(parent->getRelativePathRepresentation());
116 if (!path.isEmpty()) {
117 path.append('/');
120 path.append(Data::createSegment(node_->getTemplateName(), name_));
121 return path.makeStringAndClear();
124 rtl::Reference< Node > ChildAccess::getNode() {
125 return node_;
128 bool ChildAccess::isFinalized() {
129 return node_->getFinalized() != Data::NO_LAYER ||
130 (parent_.is() && parent_->isFinalized());
133 OUString ChildAccess::getNameInternal() {
134 return name_;
137 rtl::Reference< RootAccess > ChildAccess::getRootAccess() {
138 return root_;
141 rtl::Reference< Access > ChildAccess::getParentAccess() {
142 return parent_;
145 void ChildAccess::acquire() throw () {
146 Access::acquire();
149 void ChildAccess::release() throw () {
150 Access::release();
153 css::uno::Reference< css::uno::XInterface > ChildAccess::getParent()
154 throw (css::uno::RuntimeException, std::exception)
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, std::exception)
165 assert(thisIs(IS_ANY));
166 osl::MutexGuard g(*lock_);
167 checkLocalizedPropertyAccess();
168 throw css::lang::NoSupportException(
169 "setParent", static_cast< cppu::OWeakObject * >(this));
172 sal_Int64 ChildAccess::getSomething(
173 css::uno::Sequence< sal_Int8 > const & aIdentifier)
174 throw (css::uno::RuntimeException, std::exception)
176 assert(thisIs(IS_ANY));
177 osl::MutexGuard g(*lock_);
178 checkLocalizedPropertyAccess();
179 return aIdentifier == getTunnelId()
180 ? reinterpret_cast< sal_Int64 >(this) : 0;
183 void ChildAccess::bind(
184 rtl::Reference< RootAccess > const & root,
185 rtl::Reference< Access > const & parent, OUString const & name)
186 throw ()
188 assert(!parent_.is() && root.is() && parent.is() && !name.isEmpty());
189 root_ = root;
190 parent_ = parent;
191 name_ = name;
194 void ChildAccess::unbind() throw () {
195 assert(parent_.is());
196 parent_->releaseChild(name_);
197 parent_.clear();
198 inTransaction_ = true;
201 void ChildAccess::committed() {
202 inTransaction_ = false;
205 void ChildAccess::setNode(rtl::Reference< Node > const & node) {
206 node_ = node;
209 void ChildAccess::setProperty(
210 css::uno::Any const & value, Modifications * localModifications)
212 assert(localModifications != nullptr);
213 Type type = TYPE_ERROR;
214 bool isNillable = false;
215 switch (node_->kind()) {
216 case Node::KIND_PROPERTY:
218 PropertyNode * prop = static_cast< PropertyNode * >(node_.get());
219 type = prop->getStaticType();
220 isNillable = prop->isNillable();
222 break;
223 case Node::KIND_LOCALIZED_PROPERTY:
225 OUString locale(getRootAccess()->getLocale());
226 if (!Components::allLocales(locale)) {
227 rtl::Reference< ChildAccess > child(getChild(locale));
228 if (child.is()) {
229 child->setProperty(value, localModifications);
230 } else {
231 insertLocalizedValueChild(
232 locale, value, localModifications);
234 return;
237 break;
238 case Node::KIND_LOCALIZED_VALUE:
240 LocalizedPropertyNode * locprop =
241 static_cast< LocalizedPropertyNode * >(getParentNode().get());
242 type = locprop->getStaticType();
243 isNillable = locprop->isNillable();
245 break;
246 default:
247 break;
249 checkValue(value, type, isNillable);
250 getParentAccess()->markChildAsModified(this);
251 changedValue_.reset(new css::uno::Any(value));
252 localModifications->add(getRelativePath());
256 css::uno::Any ChildAccess::asValue()
258 if (changedValue_.get() != nullptr) {
259 return *changedValue_;
261 css::uno::Any value;
262 if (!asSimpleValue(node_, value, getComponents()))
264 if (node_->kind() == 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();
274 value = css::uno::makeAny(
275 css::uno::Reference< css::uno::XInterface >(
276 static_cast< cppu::OWeakObject * >(this)));
278 return value;
281 /// Can we quickly extract a simple value into value ? if so returns true
282 bool ChildAccess::asSimpleValue(const rtl::Reference< Node > &rNode,
283 css::uno::Any &value,
284 Components &components)
286 switch (rNode->kind()) {
287 case Node::KIND_PROPERTY:
288 value = static_cast< PropertyNode * >(rNode.get())->getValue(components);
289 return true;
290 case Node::KIND_LOCALIZED_VALUE:
291 value = static_cast< LocalizedValueNode * >(rNode.get())->getValue();
292 return true;
293 default:
294 return false;
298 void ChildAccess::commitChanges(bool valid, Modifications * globalModifications)
300 assert(globalModifications != nullptr);
301 commitChildChanges(valid, globalModifications);
302 if (valid && changedValue_.get() != nullptr) {
303 std::vector<OUString> path(getAbsolutePath());
304 getComponents().addModification(path);
305 globalModifications->add(path);
306 switch (node_->kind()) {
307 case Node::KIND_PROPERTY:
308 static_cast< PropertyNode * >(node_.get())->setValue(
309 Data::NO_LAYER, *changedValue_);
310 break;
311 case Node::KIND_LOCALIZED_VALUE:
312 static_cast< LocalizedValueNode * >(node_.get())->setValue(
313 Data::NO_LAYER, *changedValue_);
314 break;
315 default:
316 assert(false); // this cannot happen
317 break;
320 changedValue_.reset();
323 ChildAccess::~ChildAccess() {
324 osl::MutexGuard g(*lock_);
325 if (parent_.is()) {
326 parent_->releaseChild(name_);
330 void ChildAccess::addTypes(std::vector< css::uno::Type > * types) const {
331 assert(types != nullptr);
332 types->push_back(cppu::UnoType< css::container::XChild >::get());
333 types->push_back(cppu::UnoType< css::lang::XUnoTunnel >::get());
336 void ChildAccess::addSupportedServiceNames(
337 std::vector<OUString> * services)
339 assert(services != nullptr);
340 services->push_back(
341 getParentNode()->kind() == Node::KIND_GROUP
342 ? OUString("com.sun.star.configuration.GroupElement")
343 : OUString("com.sun.star.configuration.SetElement"));
346 css::uno::Any ChildAccess::queryInterface(css::uno::Type const & aType)
347 throw (css::uno::RuntimeException, std::exception)
349 assert(thisIs(IS_ANY));
350 osl::MutexGuard g(*lock_);
351 checkLocalizedPropertyAccess();
352 css::uno::Any res(Access::queryInterface(aType));
353 return res.hasValue()
354 ? res
355 : cppu::queryInterface(
356 aType, static_cast< css::container::XChild * >(this),
357 static_cast< css::lang::XUnoTunnel * >(this));
362 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */