lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / configmgr / source / childaccess.cxx
blob9433548e8e559d7ecdb78c3c5fa54d29cf3f55c3
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 > const & 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()
155 assert(thisIs(IS_ANY));
156 osl::MutexGuard g(*lock_);
157 checkLocalizedPropertyAccess();
158 return static_cast< cppu::OWeakObject * >(parent_.get());
161 void ChildAccess::setParent(css::uno::Reference< css::uno::XInterface > const &)
163 assert(thisIs(IS_ANY));
164 osl::MutexGuard g(*lock_);
165 checkLocalizedPropertyAccess();
166 throw css::lang::NoSupportException(
167 "setParent", static_cast< cppu::OWeakObject * >(this));
170 sal_Int64 ChildAccess::getSomething(
171 css::uno::Sequence< sal_Int8 > const & aIdentifier)
173 assert(thisIs(IS_ANY));
174 osl::MutexGuard g(*lock_);
175 checkLocalizedPropertyAccess();
176 return aIdentifier == getTunnelId()
177 ? reinterpret_cast< sal_Int64 >(this) : 0;
180 void ChildAccess::bind(
181 rtl::Reference< RootAccess > const & root,
182 rtl::Reference< Access > const & parent, OUString const & name)
183 throw ()
185 assert(!parent_.is() && root.is() && parent.is() && !name.isEmpty());
186 root_ = root;
187 parent_ = parent;
188 name_ = name;
191 void ChildAccess::unbind() throw () {
192 assert(parent_.is());
193 parent_->releaseChild(name_);
194 parent_.clear();
195 inTransaction_ = true;
198 void ChildAccess::committed() {
199 inTransaction_ = false;
202 void ChildAccess::setNode(rtl::Reference< Node > const & node) {
203 node_ = node;
206 void ChildAccess::setProperty(
207 css::uno::Any const & value, Modifications * localModifications)
209 assert(localModifications != nullptr);
210 Type type = TYPE_ERROR;
211 bool isNillable = false;
212 switch (node_->kind()) {
213 case Node::KIND_PROPERTY:
215 PropertyNode * prop = static_cast< PropertyNode * >(node_.get());
216 type = prop->getStaticType();
217 isNillable = prop->isNillable();
219 break;
220 case Node::KIND_LOCALIZED_PROPERTY:
222 OUString locale(getRootAccess()->getLocale());
223 if (!Components::allLocales(locale)) {
224 rtl::Reference< ChildAccess > child(getChild(locale));
225 if (child.is()) {
226 child->setProperty(value, localModifications);
227 } else {
228 insertLocalizedValueChild(
229 locale, value, localModifications);
231 return;
234 break;
235 case Node::KIND_LOCALIZED_VALUE:
237 LocalizedPropertyNode * locprop =
238 static_cast< LocalizedPropertyNode * >(getParentNode().get());
239 type = locprop->getStaticType();
240 isNillable = locprop->isNillable();
242 break;
243 default:
244 break;
246 checkValue(value, type, isNillable);
247 getParentAccess()->markChildAsModified(this);
248 changedValue_.reset(new css::uno::Any(value));
249 localModifications->add(getRelativePath());
253 css::uno::Any ChildAccess::asValue()
255 if (changedValue_ != nullptr)
257 return *changedValue_;
259 css::uno::Any value;
260 if (!asSimpleValue(node_, value, getComponents()))
262 if (node_->kind() == Node::KIND_LOCALIZED_PROPERTY)
264 OUString locale(getRootAccess()->getLocale());
265 if (!Components::allLocales(locale)) {
266 rtl::Reference< ChildAccess > child(getChild("*" + locale));
267 // As a last resort, return a nil value even though it may be
268 // illegal for the given property:
269 return child.is() ? child->asValue() : css::uno::Any();
272 value <<= css::uno::Reference< css::uno::XInterface >(
273 static_cast< cppu::OWeakObject * >(this));
275 return value;
278 /// Can we quickly extract a simple value into value ? if so returns true
279 bool ChildAccess::asSimpleValue(const rtl::Reference< Node > &rNode,
280 css::uno::Any &value,
281 Components &components)
283 switch (rNode->kind()) {
284 case Node::KIND_PROPERTY:
285 value = static_cast< PropertyNode * >(rNode.get())->getValue(components);
286 return true;
287 case Node::KIND_LOCALIZED_VALUE:
288 value = static_cast< LocalizedValueNode * >(rNode.get())->getValue();
289 return true;
290 default:
291 return false;
295 void ChildAccess::commitChanges(bool valid, Modifications * globalModifications)
297 assert(globalModifications != nullptr);
298 commitChildChanges(valid, globalModifications);
299 if (valid && changedValue_ != nullptr)
301 std::vector<OUString> path(getAbsolutePath());
302 getComponents().addModification(path);
303 globalModifications->add(path);
304 switch (node_->kind()) {
305 case Node::KIND_PROPERTY:
306 static_cast< PropertyNode * >(node_.get())->setValue(
307 Data::NO_LAYER, *changedValue_);
308 break;
309 case Node::KIND_LOCALIZED_VALUE:
310 static_cast< LocalizedValueNode * >(node_.get())->setValue(
311 Data::NO_LAYER, *changedValue_);
312 break;
313 default:
314 assert(false); // this cannot happen
315 break;
318 changedValue_.reset();
321 ChildAccess::~ChildAccess() {
322 osl::MutexGuard g(*lock_);
323 if (parent_.is()) {
324 parent_->releaseChild(name_);
328 void ChildAccess::addTypes(std::vector< css::uno::Type > * types) const {
329 assert(types != nullptr);
330 types->push_back(cppu::UnoType< css::container::XChild >::get());
331 types->push_back(cppu::UnoType< css::lang::XUnoTunnel >::get());
334 void ChildAccess::addSupportedServiceNames(
335 std::vector<OUString> * services)
337 assert(services != nullptr);
338 services->push_back(
339 getParentNode()->kind() == Node::KIND_GROUP
340 ? OUString("com.sun.star.configuration.GroupElement")
341 : OUString("com.sun.star.configuration.SetElement"));
344 css::uno::Any ChildAccess::queryInterface(css::uno::Type const & aType)
346 assert(thisIs(IS_ANY));
347 osl::MutexGuard g(*lock_);
348 checkLocalizedPropertyAccess();
349 css::uno::Any res(Access::queryInterface(aType));
350 return res.hasValue()
351 ? res
352 : cppu::queryInterface(
353 aType, static_cast< css::container::XChild * >(this),
354 static_cast< css::lang::XUnoTunnel * >(this));
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */