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
59css::uno::Sequence< sal_Int8 > const & ChildAccess::getUnoTunnelId()
60{
61 static const comphelper::UnoIdInit theChildAccessUnoTunnelId;
62 return theChildAccessUnoTunnelId.getSeq();
63}
64
66 Components & components, rtl::Reference< RootAccess > const & root,
67 rtl::Reference< Access > const & parent, OUString name,
68 rtl::Reference< Node > const & node):
69 Access(components), root_(root), parent_(parent), name_(std::move(name)), node_(node),
70 inTransaction_(false),
71 lock_( lock() )
72{
73 assert(root.is() && parent.is() && node.is());
74}
75
77 Components & components, rtl::Reference< RootAccess > const & root,
78 rtl::Reference< Node > const & node):
79 Access(components), root_(root), node_(node), inTransaction_(false),
80 lock_( lock() )
81{
82 assert(root.is() && node.is());
83}
84
85std::vector<OUString> ChildAccess::getAbsolutePath() {
87 assert(parent.is());
88 std::vector<OUString> path(parent->getAbsolutePath());
89 path.push_back(name_);
90 return path;
91}
92
93std::vector<OUString> ChildAccess::getRelativePath() {
94 std::vector<OUString> path;
96 if (parent.is()) {
97 path = parent->getRelativePath();
98 }
99 path.push_back(name_);
100 return path;
101}
102
104 OUStringBuffer path(128);
106 if (parent.is()) {
107 path.append(parent->getRelativePathRepresentation());
108 if (!path.isEmpty()) {
109 path.append('/');
110 }
111 }
112 path.append(Data::createSegment(node_->getTemplateName(), name_));
113 return path.makeStringAndClear();
114}
115
117 return node_;
118}
119
121 return node_->getFinalized() != Data::NO_LAYER ||
122 (parent_.is() && parent_->isFinalized());
123}
124
126 return name_;
127}
128
130 return root_;
131}
132
134 return parent_;
135}
136
137void ChildAccess::acquire() noexcept {
139}
140
141void ChildAccess::release() noexcept {
143}
144
145css::uno::Reference< css::uno::XInterface > ChildAccess::getParent()
146{
147 assert(thisIs(IS_ANY));
148 osl::MutexGuard g(*lock_);
150 return static_cast< cppu::OWeakObject * >(parent_.get());
151}
152
153void ChildAccess::setParent(css::uno::Reference< css::uno::XInterface > const &)
154{
155 assert(thisIs(IS_ANY));
156 osl::MutexGuard g(*lock_);
158 throw css::lang::NoSupportException(
159 "setParent", static_cast< cppu::OWeakObject * >(this));
160}
161
163 css::uno::Sequence< sal_Int8 > const & aIdentifier)
164{
165 assert(thisIs(IS_ANY));
166 osl::MutexGuard g(*lock_);
168 return comphelper::getSomethingImpl(aIdentifier, this);
169}
170
172 rtl::Reference< RootAccess > const & root,
173 rtl::Reference< Access > const & parent, OUString const & name)
174 noexcept
175{
176 assert(!parent_.is() && root.is() && parent.is() && !name.isEmpty());
177 root_ = root;
178 parent_ = parent;
179 name_ = name;
180}
181
182void ChildAccess::unbind() noexcept {
183 assert(parent_.is());
184 parent_->releaseChild(name_);
185 parent_.clear();
186 inTransaction_ = true;
187}
188
190 inTransaction_ = false;
191}
192
194 node_ = node;
195}
196
198 css::uno::Any const & value, Modifications * localModifications)
199{
200 assert(localModifications != nullptr);
202 bool isNillable = false;
203 switch (node_->kind()) {
205 {
206 PropertyNode * prop = static_cast< PropertyNode * >(node_.get());
207 type = prop->getStaticType();
208 isNillable = prop->isNillable();
209 }
210 break;
212 {
213 OUString locale(getRootAccess()->getLocale());
214 if (!Components::allLocales(locale)) {
216 if (child.is()) {
217 child->setProperty(value, localModifications);
218 } else {
220 locale, value, localModifications);
221 }
222 return;
223 }
224 }
225 break;
227 {
228 LocalizedPropertyNode * locprop =
229 static_cast< LocalizedPropertyNode * >(getParentNode().get());
230 type = locprop->getStaticType();
231 isNillable = locprop->isNillable();
232 }
233 break;
234 default:
235 break;
236 }
237 checkValue(value, type, isNillable);
238 getParentAccess()->markChildAsModified(this);
239 changedValue_.emplace(value);
240 localModifications->add(getRelativePath());
241}
242
243
244css::uno::Any ChildAccess::asValue()
245{
246 if (changedValue_)
247 {
248 return *changedValue_;
249 }
250 css::uno::Any value;
252 {
254 {
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();
261 }
262 }
263 value <<= css::uno::Reference< css::uno::XInterface >(
264 static_cast< cppu::OWeakObject * >(this));
265 }
266 return value;
267}
268
271 css::uno::Any &value,
272 Components &components)
273{
274 switch (rNode->kind()) {
276 value = static_cast< PropertyNode * >(rNode.get())->getValue(components);
277 return true;
279 value = static_cast< LocalizedValueNode * >(rNode.get())->getValue();
280 return true;
281 default:
282 return false;
283 }
284}
285
286void ChildAccess::commitChanges(bool valid, Modifications * globalModifications)
287{
288 assert(globalModifications != nullptr);
289 commitChildChanges(valid, globalModifications);
290 if (valid && changedValue_)
291 {
292 std::vector<OUString> path(getAbsolutePath());
294 globalModifications->add(path);
295 switch (node_->kind()) {
297 static_cast< PropertyNode * >(node_.get())->setValue(
299 break;
301 static_cast< LocalizedValueNode * >(node_.get())->setValue(
303 break;
304 default:
305 assert(false); // this cannot happen
306 break;
307 }
308 }
309 changedValue_.reset();
310}
311
313 osl::MutexGuard g(*lock_);
314 if (parent_.is()) {
315 parent_->releaseChild(name_);
316 }
317}
318
319void ChildAccess::addTypes(std::vector< css::uno::Type > * types) const {
320 assert(types != nullptr);
323}
324
326 std::vector<OUString> * services)
327{
328 assert(services != nullptr);
329 services->push_back(
331 ? OUString("com.sun.star.configuration.GroupElement")
332 : OUString("com.sun.star.configuration.SetElement"));
333}
334
335css::uno::Any ChildAccess::queryInterface(css::uno::Type const & aType)
336{
337 assert(thisIs(IS_ANY));
338 osl::MutexGuard g(*lock_);
340 css::uno::Any res(Access::queryInterface(aType));
341 return res.hasValue()
342 ? res
344 aType, static_cast< css::container::XChild * >(this),
345 static_cast< css::lang::XUnoTunnel * >(this));
346}
347
348}
349
350/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const css::uno::Sequence< sal_Int8 > & getSeq() const
void commitChildChanges(bool valid, Modifications *globalModifications)
Definition: access.cxx:1546
Components & getComponents() const
Definition: access.hxx:320
void checkValue(css::uno::Any const &value, Type type, bool nillable)
Definition: access.cxx:1478
rtl::Reference< Node > getParentNode()
Definition: access.cxx:1361
void insertLocalizedValueChild(OUString const &name, css::uno::Any const &value, Modifications *localModifications)
Definition: access.cxx:1512
virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const &aType) override
Definition: access.cxx:1293
bool thisIs(int what)
Definition: access.cxx:2193
rtl::Reference< ChildAccess > getChild(OUString const &name)
Definition: access.cxx:1366
void checkLocalizedPropertyAccess()
Definition: access.cxx:1351
virtual std::vector< OUString > getRelativePath() override
Definition: childaccess.cxx:93
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
virtual rtl::Reference< RootAccess > getRootAccess() override
virtual std::vector< OUString > getAbsolutePath() override
Definition: childaccess.cxx:85
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
static css::uno::Sequence< sal_Int8 > const & getUnoTunnelId()
Definition: childaccess.cxx:59
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:65
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_
virtual sal_Int64 SAL_CALL getSomething(css::uno::Sequence< sal_Int8 > const &aIdentifier) override
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()
sal_Int64 getSomethingImpl(const css::uno::Sequence< sal_Int8 > &rId, T *pThis, FallbackToGetSomethingOf< Base >={})
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_