LibreOffice Module configmgr (master) 1
childaccess.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <cassert>
23#include <utility>
24#include <vector>
25
26#include <com/sun/star/container/XChild.hpp>
27#include <com/sun/star/lang/NoSupportException.hpp>
28#include <com/sun/star/lang/XUnoTunnel.hpp>
29#include <com/sun/star/uno/Any.hxx>
30#include <com/sun/star/uno/Reference.hxx>
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>
36#include <cppuhelper/weak.hxx>
38#include <osl/mutex.hxx>
39#include <rtl/ref.hxx>
40#include <rtl/ustrbuf.hxx>
41#include <rtl/ustring.hxx>
42#include <sal/types.h>
43
44#include "access.hxx"
45#include "childaccess.hxx"
46#include "components.hxx"
47#include "data.hxx"
50#include "lock.hxx"
51#include "modifications.hxx"
52#include "node.hxx"
53#include "propertynode.hxx"
54#include "rootaccess.hxx"
55#include "type.hxx"
56
57namespace configmgr {
58
60 Components & components, rtl::Reference< RootAccess > const & root,
61 rtl::Reference< Access > const & parent, OUString name,
62 rtl::Reference< Node > const & node):
63 Access(components), root_(root), parent_(parent), name_(std::move(name)), node_(node),
64 inTransaction_(false),
65 lock_( lock() )
66{
67 assert(root.is() && parent.is() && node.is());
68}
69
71 Components & components, rtl::Reference< RootAccess > const & root,
72 rtl::Reference< Node > const & node):
73 Access(components), root_(root), node_(node), inTransaction_(false),
74 lock_( lock() )
75{
76 assert(root.is() && node.is());
77}
78
79std::vector<OUString> ChildAccess::getAbsolutePath() {
81 assert(parent.is());
82 std::vector<OUString> path(parent->getAbsolutePath());
83 path.push_back(name_);
84 return path;
85}
86
87std::vector<OUString> ChildAccess::getRelativePath() {
88 std::vector<OUString> path;
90 if (parent.is()) {
91 path = parent->getRelativePath();
92 }
93 path.push_back(name_);
94 return path;
95}
96
98 OUStringBuffer path(128);
100 if (parent.is()) {
101 path.append(parent->getRelativePathRepresentation());
102 if (!path.isEmpty()) {
103 path.append('/');
104 }
105 }
106 path.append(Data::createSegment(node_->getTemplateName(), name_));
107 return path.makeStringAndClear();
108}
109
111 return node_;
112}
113
115 return node_->getFinalized() != Data::NO_LAYER ||
116 (parent_.is() && parent_->isFinalized());
117}
118
120 return name_;
121}
122
124 return root_;
125}
126
128 return parent_;
129}
130
131void ChildAccess::acquire() noexcept {
133}
134
135void ChildAccess::release() noexcept {
137}
138
139css::uno::Reference< css::uno::XInterface > ChildAccess::getParent()
140{
141 assert(thisIs(IS_ANY));
142 osl::MutexGuard g(*lock_);
144 return cppu::getXWeak(parent_.get());
145}
146
147void ChildAccess::setParent(css::uno::Reference< css::uno::XInterface > const &)
148{
149 assert(thisIs(IS_ANY));
150 osl::MutexGuard g(*lock_);
152 throw css::lang::NoSupportException(
153 "setParent", getXWeak());
154}
155
157 rtl::Reference< RootAccess > const & root,
158 rtl::Reference< Access > const & parent, OUString const & name)
159 noexcept
160{
161 assert(!parent_.is() && root.is() && parent.is() && !name.isEmpty());
162 root_ = root;
163 parent_ = parent;
164 name_ = name;
165}
166
167void ChildAccess::unbind() noexcept {
168 assert(parent_.is());
169 parent_->releaseChild(name_);
170 parent_.clear();
171 inTransaction_ = true;
172}
173
175 inTransaction_ = false;
176}
177
179 node_ = node;
180}
181
183 css::uno::Any const & value, Modifications * localModifications)
184{
185 assert(localModifications != nullptr);
187 bool isNillable = false;
188 switch (node_->kind()) {
190 {
191 PropertyNode * prop = static_cast< PropertyNode * >(node_.get());
192 type = prop->getStaticType();
193 isNillable = prop->isNillable();
194 }
195 break;
197 {
198 OUString locale(getRootAccess()->getLocale());
199 if (!Components::allLocales(locale)) {
201 if (child.is()) {
202 child->setProperty(value, localModifications);
203 } else {
205 locale, value, localModifications);
206 }
207 return;
208 }
209 }
210 break;
212 {
213 LocalizedPropertyNode * locprop =
214 static_cast< LocalizedPropertyNode * >(getParentNode().get());
215 type = locprop->getStaticType();
216 isNillable = locprop->isNillable();
217 }
218 break;
219 default:
220 break;
221 }
222 checkValue(value, type, isNillable);
223 getParentAccess()->markChildAsModified(this);
224 changedValue_.emplace(value);
225 localModifications->add(getRelativePath());
226}
227
228
229css::uno::Any ChildAccess::asValue()
230{
231 if (changedValue_)
232 {
233 return *changedValue_;
234 }
235 css::uno::Any value;
237 {
239 {
240 OUString locale(getRootAccess()->getLocale());
241 if (!Components::allLocales(locale)) {
242 rtl::Reference< ChildAccess > child(getChild("*" + locale));
243 // As a last resort, return a nil value even though it may be
244 // illegal for the given property:
245 return child.is() ? child->asValue() : css::uno::Any();
246 }
247 }
248 value <<= css::uno::Reference(getXWeak());
249 }
250 return value;
251}
252
255 css::uno::Any &value,
256 Components &components)
257{
258 switch (rNode->kind()) {
260 value = static_cast< PropertyNode * >(rNode.get())->getValue(components);
261 return true;
263 value = static_cast< LocalizedValueNode * >(rNode.get())->getValue();
264 return true;
265 default:
266 return false;
267 }
268}
269
270void ChildAccess::commitChanges(bool valid, Modifications * globalModifications)
271{
272 assert(globalModifications != nullptr);
273 commitChildChanges(valid, globalModifications);
274 if (valid && changedValue_)
275 {
276 std::vector<OUString> path(getAbsolutePath());
278 globalModifications->add(path);
279 switch (node_->kind()) {
281 static_cast< PropertyNode * >(node_.get())->setValue(
283 break;
285 static_cast< LocalizedValueNode * >(node_.get())->setValue(
287 break;
288 default:
289 assert(false); // this cannot happen
290 break;
291 }
292 }
293 changedValue_.reset();
294}
295
297 osl::MutexGuard g(*lock_);
298 if (parent_.is()) {
299 parent_->releaseChild(name_);
300 }
301}
302
303void ChildAccess::addTypes(std::vector< css::uno::Type > * types) const {
304 assert(types != nullptr);
307}
308
310 std::vector<OUString> * services)
311{
312 assert(services != nullptr);
313 services->push_back(
315 ? OUString("com.sun.star.configuration.GroupElement")
316 : OUString("com.sun.star.configuration.SetElement"));
317}
318
319css::uno::Any ChildAccess::queryInterface(css::uno::Type const & aType)
320{
321 assert(thisIs(IS_ANY));
322 osl::MutexGuard g(*lock_);
324 css::uno::Any res(Access::queryInterface(aType));
325 return res.hasValue()
326 ? res
328 aType, static_cast< css::container::XChild * >(this));
329}
330
331}
332
333/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void commitChildChanges(bool valid, Modifications *globalModifications)
Definition: access.cxx:1556
Components & getComponents() const
Definition: access.hxx:320
void checkValue(css::uno::Any const &value, Type type, bool nillable)
Definition: access.cxx:1488
rtl::Reference< Node > getParentNode()
Definition: access.cxx:1362
void insertLocalizedValueChild(OUString const &name, css::uno::Any const &value, Modifications *localModifications)
Definition: access.cxx:1522
virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const &aType) override
Definition: access.cxx:1294
bool thisIs(int what)
Definition: access.cxx:2201
rtl::Reference< ChildAccess > getChild(OUString const &name)
Definition: access.cxx:1367
void checkLocalizedPropertyAccess()
Definition: access.cxx:1352
virtual std::vector< OUString > getRelativePath() override
Definition: childaccess.cxx:87
void unbind() noexcept
void setProperty(css::uno::Any const &value, Modifications *localModifications)
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL getParent() override
virtual void addTypes(std::vector< css::uno::Type > *types) const override
virtual void SAL_CALL setParent(css::uno::Reference< css::uno::XInterface > const &) override
rtl::Reference< Access > parent_
rtl::Reference< RootAccess > root_
virtual ~ChildAccess() override
virtual rtl::Reference< Node > getNode() override
void bind(rtl::Reference< RootAccess > const &root, rtl::Reference< Access > const &parent, OUString const &name) noexcept
virtual OUString getRelativePathRepresentation() override
Definition: childaccess.cxx:97
virtual rtl::Reference< RootAccess > getRootAccess() override
virtual std::vector< OUString > getAbsolutePath() override
Definition: childaccess.cxx:79
virtual void SAL_CALL release() noexcept override
void commitChanges(bool valid, Modifications *globalModifications)
css::uno::Any asValue()
std::shared_ptr< osl::Mutex > lock_
virtual void SAL_CALL acquire() noexcept override
virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const &aType) override
ChildAccess(Components &components, rtl::Reference< RootAccess > const &root, rtl::Reference< Access > const &parent, OUString name, rtl::Reference< Node > const &node)
Definition: childaccess.cxx:59
virtual const OUString & getNameInternal() override
std::optional< css::uno::Any > changedValue_
virtual void addSupportedServiceNames(std::vector< OUString > *services) override
static bool asSimpleValue(const rtl::Reference< Node > &rNode, css::uno::Any &value, Components &components)
Can we quickly extract a simple value into value ? if so returns true.
virtual rtl::Reference< Access > getParentAccess() override
rtl::Reference< Node > node_
void setNode(rtl::Reference< Node > const &node)
virtual bool isFinalized() override
static bool allLocales(std::u16string_view locale)
Definition: components.cxx:207
void addModification(std::vector< OUString > const &path)
Definition: components.cxx:270
void add(std::vector< OUString > const &path)
@ KIND_LOCALIZED_PROPERTY
Definition: node.hxx:35
@ KIND_LOCALIZED_VALUE
Definition: node.hxx:35
Type getStaticType() const
virtual void SAL_CALL acquire() SAL_NOEXCEPT SAL_OVERRIDE
virtual void SAL_CALL release() SAL_NOEXCEPT SAL_OVERRIDE
Any value
OUString name
Definition: components.cxx:85
std::shared_ptr< osl::Mutex > lock_
const LanguageTag & getLocale()
std::shared_ptr< osl::Mutex > const & lock()
Definition: lock.cxx:28
@ TYPE_ERROR
Definition: type.hxx:33
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
css::beans::Optional< css::uno::Any > getValue(std::u16string_view id)
rtl::Reference< RootAccess > root_
RegError REGISTRY_CALLTYPE setValue(RegKeyHandle hKey, rtl_uString *keyName, RegValueType valueType, RegValue pData, sal_uInt32 valueSize)
static OUString createSegment(std::u16string_view templateName, OUString const &name)
Definition: data.cxx:81
ResultType type
OUString name_