bump product version to 5.0.4.1
[LibreOffice.git] / configmgr / source / childaccess.cxx
bloba9771ff78d2087c564446efdd8d12abe8dddd601
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 "path.hxx"
57 #include "propertynode.hxx"
58 #include "rootaccess.hxx"
59 #include "setnode.hxx"
60 #include "type.hxx"
62 namespace configmgr {
64 namespace
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, OUString const & name,
77 rtl::Reference< Node > const & node):
78 Access(components), root_(root), parent_(parent), name_(name), node_(node),
79 inTransaction_(false)
81 lock_ = lock();
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)
90 lock_ = lock();
91 assert(root.is() && node.is());
94 Path ChildAccess::getAbsolutePath() {
95 rtl::Reference< Access > parent(getParentAccess());
96 assert(parent.is());
97 Path path(parent->getAbsolutePath());
98 path.push_back(name_);
99 return path;
102 Path ChildAccess::getRelativePath() {
103 Path path;
104 rtl::Reference< Access > parent(getParentAccess());
105 if (parent.is()) {
106 path = parent->getRelativePath();
108 path.push_back(name_);
109 return path;
112 OUString ChildAccess::getRelativePathRepresentation() {
113 OUStringBuffer path;
114 rtl::Reference< Access > parent(getParentAccess());
115 if (parent.is()) {
116 path.append(parent->getRelativePathRepresentation());
117 if (!path.isEmpty()) {
118 path.append('/');
121 path.append(Data::createSegment(node_->getTemplateName(), name_));
122 return path.makeStringAndClear();
125 rtl::Reference< Node > ChildAccess::getNode() {
126 return node_;
129 bool ChildAccess::isFinalized() {
130 return node_->getFinalized() != Data::NO_LAYER ||
131 (parent_.is() && parent_->isFinalized());
134 OUString ChildAccess::getNameInternal() {
135 return name_;
138 rtl::Reference< RootAccess > ChildAccess::getRootAccess() {
139 return root_;
142 rtl::Reference< Access > ChildAccess::getParentAccess() {
143 return parent_;
146 void ChildAccess::acquire() throw () {
147 Access::acquire();
150 void ChildAccess::release() throw () {
151 Access::release();
154 css::uno::Reference< css::uno::XInterface > ChildAccess::getParent()
155 throw (css::uno::RuntimeException, std::exception)
157 assert(thisIs(IS_ANY));
158 osl::MutexGuard g(*lock_);
159 checkLocalizedPropertyAccess();
160 return static_cast< cppu::OWeakObject * >(parent_.get());
163 void ChildAccess::setParent(css::uno::Reference< css::uno::XInterface > const &)
164 throw (css::lang::NoSupportException, css::uno::RuntimeException, std::exception)
166 assert(thisIs(IS_ANY));
167 osl::MutexGuard g(*lock_);
168 checkLocalizedPropertyAccess();
169 throw css::lang::NoSupportException(
170 "setParent", static_cast< cppu::OWeakObject * >(this));
173 sal_Int64 ChildAccess::getSomething(
174 css::uno::Sequence< sal_Int8 > const & aIdentifier)
175 throw (css::uno::RuntimeException, std::exception)
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, OUString const & name)
187 throw ()
189 assert(!parent_.is() && root.is() && parent.is() && !name.isEmpty());
190 root_ = root;
191 parent_ = parent;
192 name_ = name;
195 void ChildAccess::unbind() throw () {
196 assert(parent_.is());
197 parent_->releaseChild(name_);
198 parent_.clear();
199 inTransaction_ = true;
202 void ChildAccess::committed() {
203 inTransaction_ = false;
206 void ChildAccess::setNode(rtl::Reference< Node > const & node) {
207 node_ = 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 = static_cast< PropertyNode * >(node_.get());
220 type = prop->getStaticType();
221 nillable = prop->isNillable();
223 break;
224 case Node::KIND_LOCALIZED_PROPERTY:
226 OUString locale(getRootAccess()->getLocale());
227 if (!Components::allLocales(locale)) {
228 rtl::Reference< ChildAccess > child(getChild(locale));
229 if (child.is()) {
230 child->setProperty(value, localModifications);
231 } else {
232 insertLocalizedValueChild(
233 locale, value, localModifications);
235 return;
238 break;
239 case Node::KIND_LOCALIZED_VALUE:
241 LocalizedPropertyNode * locprop =
242 static_cast< LocalizedPropertyNode * >(getParentNode().get());
243 type = locprop->getStaticType();
244 nillable = locprop->isNillable();
246 break;
247 default:
248 break;
250 checkValue(value, type, nillable);
251 getParentAccess()->markChildAsModified(this);
252 changedValue_.reset(new css::uno::Any(value));
253 localModifications->add(getRelativePath());
257 css::uno::Any ChildAccess::asValue()
259 if (changedValue_.get() != 0) {
260 return *changedValue_;
262 css::uno::Any value;
263 if (!asSimpleValue(node_, value, getComponents()))
265 if (node_->kind() == Node::KIND_LOCALIZED_PROPERTY)
267 OUString locale(getRootAccess()->getLocale());
268 if (!Components::allLocales(locale)) {
269 rtl::Reference< ChildAccess > child(getChild("*" + locale));
270 // As a last resort, return a nil value even though it may be
271 // illegal for the given property:
272 if (child.is())
273 return child->asValue();
276 value = css::uno::makeAny(
277 css::uno::Reference< css::uno::XInterface >(
278 static_cast< cppu::OWeakObject * >(this)));
280 return value;
283 /// Can we quickly extract a simple value into value ? if so returns true
284 bool ChildAccess::asSimpleValue(const rtl::Reference< Node > &rNode,
285 css::uno::Any &value,
286 Components &components)
288 switch (rNode->kind()) {
289 case Node::KIND_PROPERTY:
290 value = static_cast< PropertyNode * >(rNode.get())->getValue(components);
291 return true;
292 case Node::KIND_LOCALIZED_VALUE:
293 value = static_cast< LocalizedValueNode * >(rNode.get())->getValue();
294 return true;
295 default:
296 return false;
300 void ChildAccess::commitChanges(bool valid, Modifications * globalModifications)
302 assert(globalModifications != 0);
303 commitChildChanges(valid, globalModifications);
304 if (valid && changedValue_.get() != 0) {
305 Path path(getAbsolutePath());
306 getComponents().addModification(path);
307 globalModifications->add(path);
308 switch (node_->kind()) {
309 case Node::KIND_PROPERTY:
310 static_cast< PropertyNode * >(node_.get())->setValue(
311 Data::NO_LAYER, *changedValue_);
312 break;
313 case Node::KIND_LOCALIZED_VALUE:
314 static_cast< LocalizedValueNode * >(node_.get())->setValue(
315 Data::NO_LAYER, *changedValue_);
316 break;
317 default:
318 assert(false); // this cannot happen
319 break;
322 changedValue_.reset();
325 ChildAccess::~ChildAccess() {
326 osl::MutexGuard g(*lock_);
327 if (parent_.is()) {
328 parent_->releaseChild(name_);
332 void ChildAccess::addTypes(std::vector< css::uno::Type > * types) const {
333 assert(types != 0);
334 types->push_back(cppu::UnoType< css::container::XChild >::get());
335 types->push_back(cppu::UnoType< css::lang::XUnoTunnel >::get());
338 void ChildAccess::addSupportedServiceNames(
339 std::vector< OUString > * services)
341 assert(services != 0);
342 services->push_back(
343 getParentNode()->kind() == Node::KIND_GROUP
344 ? OUString("com.sun.star.configuration.GroupElement")
345 : OUString("com.sun.star.configuration.SetElement"));
348 css::uno::Any ChildAccess::queryInterface(css::uno::Type const & aType)
349 throw (css::uno::RuntimeException, std::exception)
351 assert(thisIs(IS_ANY));
352 osl::MutexGuard g(*lock_);
353 checkLocalizedPropertyAccess();
354 css::uno::Any res(Access::queryInterface(aType));
355 return res.hasValue()
356 ? res
357 : cppu::queryInterface(
358 aType, static_cast< css::container::XChild * >(this),
359 static_cast< css::lang::XUnoTunnel * >(this));
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */