Line data Source code
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 <vector>
24 :
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>
45 :
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"
61 :
62 : namespace configmgr {
63 :
64 : namespace
65 : {
66 : class theChildAccessUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theChildAccessUnoTunnelId > {};
67 : }
68 :
69 10232 : css::uno::Sequence< sal_Int8 > ChildAccess::getTunnelId()
70 : {
71 10232 : return theChildAccessUnoTunnelId::get().getSeq();
72 : }
73 :
74 13158498 : 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 13158498 : inTransaction_(false)
80 : {
81 13158498 : lock_ = lock();
82 : assert(root.is() && parent.is() && node.is());
83 13158498 : }
84 :
85 5116 : ChildAccess::ChildAccess(
86 : Components & components, rtl::Reference< RootAccess > const & root,
87 : rtl::Reference< Node > const & node):
88 5116 : Access(components), root_(root), node_(node), inTransaction_(false)
89 : {
90 5116 : lock_ = lock();
91 : assert(root.is() && node.is());
92 5116 : }
93 :
94 1113172 : Path ChildAccess::getAbsolutePath() {
95 : assert(getParentAccess().is());
96 1113172 : Path path(getParentAccess()->getAbsolutePath());
97 1113172 : path.push_back(name_);
98 1113172 : return path;
99 : }
100 :
101 688470 : Path ChildAccess::getRelativePath() {
102 688470 : Path path;
103 1376940 : rtl::Reference< Access > parent(getParentAccess());
104 688470 : if (parent.is()) {
105 688266 : path = parent->getRelativePath();
106 : }
107 688470 : path.push_back(name_);
108 1376940 : return path;
109 : }
110 :
111 85032 : OUString ChildAccess::getRelativePathRepresentation() {
112 85032 : OUStringBuffer path;
113 170064 : rtl::Reference< Access > parent(getParentAccess());
114 85032 : if (parent.is()) {
115 85032 : path.append(parent->getRelativePathRepresentation());
116 85032 : if (!path.isEmpty()) {
117 40174 : path.append('/');
118 : }
119 : }
120 85032 : path.append(Data::createSegment(node_->getTemplateName(), name_));
121 170064 : return path.makeStringAndClear();
122 : }
123 :
124 48042122 : rtl::Reference< Node > ChildAccess::getNode() {
125 48042122 : return node_;
126 : }
127 :
128 1853427 : bool ChildAccess::isFinalized() {
129 3706854 : return node_->getFinalized() != Data::NO_LAYER ||
130 5560077 : (parent_.is() && parent_->isFinalized());
131 : }
132 :
133 3818131 : OUString ChildAccess::getNameInternal() {
134 3818131 : return name_;
135 : }
136 :
137 12171232 : rtl::Reference< RootAccess > ChildAccess::getRootAccess() {
138 12171232 : return root_;
139 : }
140 :
141 4222237 : rtl::Reference< Access > ChildAccess::getParentAccess() {
142 4222237 : return parent_;
143 : }
144 :
145 66876015 : void ChildAccess::acquire() throw () {
146 66876015 : Access::acquire();
147 66876014 : }
148 :
149 66875475 : void ChildAccess::release() throw () {
150 66875475 : Access::release();
151 66875475 : }
152 :
153 0 : css::uno::Reference< css::uno::XInterface > ChildAccess::getParent()
154 : throw (css::uno::RuntimeException, std::exception)
155 : {
156 : assert(thisIs(IS_ANY));
157 0 : osl::MutexGuard g(*lock_);
158 0 : checkLocalizedPropertyAccess();
159 0 : return static_cast< cppu::OWeakObject * >(parent_.get());
160 : }
161 :
162 0 : void ChildAccess::setParent(css::uno::Reference< css::uno::XInterface > const &)
163 : throw (css::lang::NoSupportException, css::uno::RuntimeException, std::exception)
164 : {
165 : assert(thisIs(IS_ANY));
166 0 : osl::MutexGuard g(*lock_);
167 0 : checkLocalizedPropertyAccess();
168 : throw css::lang::NoSupportException(
169 0 : "setParent", static_cast< cppu::OWeakObject * >(this));
170 : }
171 :
172 5116 : sal_Int64 ChildAccess::getSomething(
173 : css::uno::Sequence< sal_Int8 > const & aIdentifier)
174 : throw (css::uno::RuntimeException, std::exception)
175 : {
176 : assert(thisIs(IS_ANY));
177 5116 : osl::MutexGuard g(*lock_);
178 5116 : checkLocalizedPropertyAccess();
179 10232 : return aIdentifier == getTunnelId()
180 10232 : ? reinterpret_cast< sal_Int64 >(this) : 0;
181 : }
182 :
183 5116 : void ChildAccess::bind(
184 : rtl::Reference< RootAccess > const & root,
185 : rtl::Reference< Access > const & parent, OUString const & name)
186 : throw ()
187 : {
188 : assert(!parent_.is() && root.is() && parent.is() && !name.isEmpty());
189 5116 : root_ = root;
190 5116 : parent_ = parent;
191 5116 : name_ = name;
192 5116 : }
193 :
194 2306 : void ChildAccess::unbind() throw () {
195 : assert(parent_.is());
196 2306 : parent_->releaseChild(name_);
197 2306 : parent_.clear();
198 2306 : inTransaction_ = true;
199 2306 : }
200 :
201 450887 : void ChildAccess::committed() {
202 450887 : inTransaction_ = false;
203 450887 : }
204 :
205 136950 : void ChildAccess::setNode(rtl::Reference< Node > const & node) {
206 136950 : node_ = node;
207 136950 : }
208 :
209 213797 : void ChildAccess::setProperty(
210 : css::uno::Any const & value, Modifications * localModifications)
211 : {
212 : assert(localModifications != 0);
213 213797 : Type type = TYPE_ERROR;
214 213797 : bool nillable = false;
215 213797 : switch (node_->kind()) {
216 : case Node::KIND_PROPERTY:
217 : {
218 205823 : PropertyNode * prop = static_cast< PropertyNode * >(node_.get());
219 205823 : type = prop->getStaticType();
220 205823 : nillable = prop->isNillable();
221 : }
222 205823 : break;
223 : case Node::KIND_LOCALIZED_PROPERTY:
224 : {
225 3987 : OUString locale(getRootAccess()->getLocale());
226 3987 : if (!Components::allLocales(locale)) {
227 3987 : rtl::Reference< ChildAccess > child(getChild(locale));
228 3987 : if (child.is()) {
229 3987 : child->setProperty(value, localModifications);
230 : } else {
231 : insertLocalizedValueChild(
232 0 : locale, value, localModifications);
233 : }
234 217784 : return;
235 0 : }
236 : }
237 0 : break;
238 : case Node::KIND_LOCALIZED_VALUE:
239 : {
240 : LocalizedPropertyNode * locprop =
241 3987 : static_cast< LocalizedPropertyNode * >(getParentNode().get());
242 3987 : type = locprop->getStaticType();
243 3987 : nillable = locprop->isNillable();
244 : }
245 3987 : break;
246 : default:
247 0 : break;
248 : }
249 209810 : checkValue(value, type, nillable);
250 209810 : getParentAccess()->markChildAsModified(this);
251 209810 : changedValue_.reset(new css::uno::Any(value));
252 209810 : localModifications->add(getRelativePath());
253 : }
254 :
255 :
256 3357232 : css::uno::Any ChildAccess::asValue()
257 : {
258 3357232 : if (changedValue_.get() != 0) {
259 24204 : return *changedValue_;
260 : }
261 3333028 : css::uno::Any value;
262 3333028 : if (!asSimpleValue(node_, value, getComponents()))
263 : {
264 1534253 : if (node_->kind() == Node::KIND_LOCALIZED_PROPERTY)
265 : {
266 480687 : OUString locale(getRootAccess()->getLocale());
267 480687 : if (!Components::allLocales(locale)) {
268 265789 : 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 265789 : if (child.is())
272 180957 : return child->asValue();
273 299730 : }
274 : }
275 2706592 : value = css::uno::makeAny(
276 : css::uno::Reference< css::uno::XInterface >(
277 1353296 : static_cast< cppu::OWeakObject * >(this)));
278 : }
279 3152071 : return value;
280 : }
281 :
282 : /// Can we quickly extract a simple value into value ? if so returns true
283 7079845 : bool ChildAccess::asSimpleValue(const rtl::Reference< Node > &rNode,
284 : css::uno::Any &value,
285 : Components &components)
286 : {
287 7079845 : switch (rNode->kind()) {
288 : case Node::KIND_PROPERTY:
289 3864061 : value = static_cast< PropertyNode * >(rNode.get())->getValue(components);
290 3864061 : return true;
291 : case Node::KIND_LOCALIZED_VALUE:
292 368477 : value = static_cast< LocalizedValueNode * >(rNode.get())->getValue();
293 368477 : return true;
294 : default:
295 2847307 : return false;
296 : }
297 : }
298 :
299 448581 : void ChildAccess::commitChanges(bool valid, Modifications * globalModifications)
300 : {
301 : assert(globalModifications != 0);
302 448581 : commitChildChanges(valid, globalModifications);
303 448581 : if (valid && changedValue_.get() != 0) {
304 209810 : Path path(getAbsolutePath());
305 209810 : getComponents().addModification(path);
306 209810 : globalModifications->add(path);
307 209810 : switch (node_->kind()) {
308 : case Node::KIND_PROPERTY:
309 205823 : static_cast< PropertyNode * >(node_.get())->setValue(
310 411646 : Data::NO_LAYER, *changedValue_);
311 205823 : break;
312 : case Node::KIND_LOCALIZED_VALUE:
313 3987 : static_cast< LocalizedValueNode * >(node_.get())->setValue(
314 7974 : Data::NO_LAYER, *changedValue_);
315 3987 : break;
316 : default:
317 : assert(false); // this cannot happen
318 0 : break;
319 209810 : }
320 : }
321 448581 : changedValue_.reset();
322 448581 : }
323 :
324 39489222 : ChildAccess::~ChildAccess() {
325 13163074 : osl::MutexGuard g(*lock_);
326 13163074 : if (parent_.is()) {
327 13160768 : parent_->releaseChild(name_);
328 13163074 : }
329 26326148 : }
330 :
331 0 : void ChildAccess::addTypes(std::vector< css::uno::Type > * types) const {
332 : assert(types != 0);
333 0 : types->push_back(cppu::UnoType< css::container::XChild >::get());
334 0 : types->push_back(cppu::UnoType< css::lang::XUnoTunnel >::get());
335 0 : }
336 :
337 10228 : void ChildAccess::addSupportedServiceNames(
338 : std::vector< OUString > * services)
339 : {
340 : assert(services != 0);
341 : services->push_back(
342 20456 : getParentNode()->kind() == Node::KIND_GROUP
343 : ? OUString("com.sun.star.configuration.GroupElement")
344 10228 : : OUString("com.sun.star.configuration.SetElement"));
345 10228 : }
346 :
347 1465792 : css::uno::Any ChildAccess::queryInterface(css::uno::Type const & aType)
348 : throw (css::uno::RuntimeException, std::exception)
349 : {
350 : assert(thisIs(IS_ANY));
351 1465792 : osl::MutexGuard g(*lock_);
352 1465792 : checkLocalizedPropertyAccess();
353 2931584 : css::uno::Any res(Access::queryInterface(aType));
354 1465792 : return res.hasValue()
355 : ? res
356 : : cppu::queryInterface(
357 : aType, static_cast< css::container::XChild * >(this),
358 2931584 : static_cast< css::lang::XUnoTunnel * >(this));
359 : }
360 :
361 : }
362 :
363 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|