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 2225 : css::uno::Sequence< sal_Int8 > ChildAccess::getTunnelId()
70 : {
71 2225 : return theChildAccessUnoTunnelId::get().getSeq();
72 : }
73 :
74 1085690 : 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 1085690 : inTransaction_(false)
80 : {
81 1085690 : lock_ = lock();
82 : assert(root.is() && parent.is() && node.is());
83 1085690 : }
84 :
85 1111 : ChildAccess::ChildAccess(
86 : Components & components, rtl::Reference< RootAccess > const & root,
87 : rtl::Reference< Node > const & node):
88 1111 : Access(components), root_(root), node_(node), inTransaction_(false)
89 : {
90 1111 : lock_ = lock();
91 : assert(root.is() && node.is());
92 1111 : }
93 :
94 82890 : Path ChildAccess::getAbsolutePath() {
95 : assert(getParentAccess().is());
96 82890 : Path path(getParentAccess()->getAbsolutePath());
97 82890 : path.push_back(name_);
98 82890 : return path;
99 : }
100 :
101 50797 : Path ChildAccess::getRelativePath() {
102 50797 : Path path;
103 50797 : rtl::Reference< Access > parent(getParentAccess());
104 50797 : if (parent.is()) {
105 50797 : path = parent->getRelativePath();
106 : }
107 50797 : path.push_back(name_);
108 50797 : return path;
109 : }
110 :
111 2571 : OUString ChildAccess::getRelativePathRepresentation() {
112 2571 : OUStringBuffer path;
113 2571 : rtl::Reference< Access > parent(getParentAccess());
114 2571 : if (parent.is()) {
115 2571 : path.append(parent->getRelativePathRepresentation());
116 2571 : if (path.getLength() != 0) {
117 1458 : path.append(sal_Unicode('/'));
118 : }
119 : }
120 2571 : path.append(Data::createSegment(node_->getTemplateName(), name_));
121 2571 : return path.makeStringAndClear();
122 : }
123 :
124 3242296 : rtl::Reference< Node > ChildAccess::getNode() {
125 3242296 : return node_;
126 : }
127 :
128 134383 : bool ChildAccess::isFinalized() {
129 134383 : return node_->getFinalized() != Data::NO_LAYER ||
130 134383 : (parent_.is() && parent_->isFinalized());
131 : }
132 :
133 302338 : OUString ChildAccess::getNameInternal() {
134 302338 : return name_;
135 : }
136 :
137 1020470 : rtl::Reference< RootAccess > ChildAccess::getRootAccess() {
138 1020470 : return root_;
139 : }
140 :
141 313759 : rtl::Reference< Access > ChildAccess::getParentAccess() {
142 313759 : return parent_;
143 : }
144 :
145 6010838 : void ChildAccess::acquire() throw () {
146 6010838 : Access::acquire();
147 6010838 : }
148 :
149 6010582 : void ChildAccess::release() throw () {
150 6010582 : Access::release();
151 6010582 : }
152 :
153 0 : css::uno::Reference< css::uno::XInterface > ChildAccess::getParent()
154 : throw (css::uno::RuntimeException)
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)
164 : {
165 : assert(thisIs(IS_ANY));
166 0 : osl::MutexGuard g(*lock_);
167 0 : checkLocalizedPropertyAccess();
168 : throw css::lang::NoSupportException(
169 : OUString("setParent"),
170 0 : static_cast< cppu::OWeakObject * >(this));
171 : }
172 :
173 1114 : sal_Int64 ChildAccess::getSomething(
174 : css::uno::Sequence< sal_Int8 > const & aIdentifier)
175 : throw (css::uno::RuntimeException)
176 : {
177 : assert(thisIs(IS_ANY));
178 1114 : osl::MutexGuard g(*lock_);
179 1114 : checkLocalizedPropertyAccess();
180 2228 : return aIdentifier == getTunnelId()
181 2228 : ? reinterpret_cast< sal_Int64 >(this) : 0;
182 : }
183 :
184 1111 : void ChildAccess::bind(
185 : rtl::Reference< RootAccess > const & root,
186 : rtl::Reference< Access > const & parent, OUString const & name)
187 : throw ()
188 : {
189 : assert(!parent_.is() && root.is() && parent.is() && !name.isEmpty());
190 1111 : root_ = root;
191 1111 : parent_ = parent;
192 1111 : name_ = name;
193 1111 : }
194 :
195 68 : void ChildAccess::unbind() throw () {
196 : assert(parent_.is());
197 68 : parent_->releaseChild(name_);
198 68 : parent_.clear();
199 68 : inTransaction_ = true;
200 68 : }
201 :
202 27339 : void ChildAccess::committed() {
203 27339 : inTransaction_ = false;
204 27339 : }
205 :
206 9886 : void ChildAccess::setNode(rtl::Reference< Node > const & node) {
207 9886 : node_ = node;
208 9886 : }
209 :
210 11926 : void ChildAccess::setProperty(
211 : css::uno::Any const & value, Modifications * localModifications)
212 : {
213 : assert(localModifications != 0);
214 11926 : Type type = TYPE_ERROR;
215 11926 : bool nillable = false;
216 11926 : switch (node_->kind()) {
217 : case Node::KIND_PROPERTY:
218 : {
219 11914 : PropertyNode * prop = dynamic_cast< PropertyNode * >(node_.get());
220 11914 : type = prop->getStaticType();
221 11914 : nillable = prop->isNillable();
222 : }
223 11914 : break;
224 : case Node::KIND_LOCALIZED_PROPERTY:
225 : {
226 6 : OUString locale(getRootAccess()->getLocale());
227 6 : if (!Components::allLocales(locale)) {
228 6 : rtl::Reference< ChildAccess > child(getChild(locale));
229 6 : if (child.is()) {
230 6 : child->setProperty(value, localModifications);
231 : } else {
232 : insertLocalizedValueChild(
233 0 : locale, value, localModifications);
234 : }
235 11916 : return;
236 6 : }
237 : }
238 0 : break;
239 : case Node::KIND_LOCALIZED_VALUE:
240 : {
241 : LocalizedPropertyNode * locprop =
242 6 : dynamic_cast< LocalizedPropertyNode * >(getParentNode().get());
243 6 : type = locprop->getStaticType();
244 6 : nillable = locprop->isNillable();
245 : }
246 6 : break;
247 : default:
248 0 : break;
249 : }
250 11920 : checkValue(value, type, nillable);
251 11904 : getParentAccess()->markChildAsModified(this);
252 11904 : changedValue_.reset(new css::uno::Any(value));
253 11904 : localModifications->add(getRelativePath());
254 : }
255 :
256 668384 : css::uno::Any ChildAccess::asValue() {
257 668384 : if (changedValue_.get() != 0) {
258 232 : return *changedValue_;
259 : }
260 668152 : switch (node_->kind()) {
261 : case Node::KIND_PROPERTY:
262 485013 : return dynamic_cast< PropertyNode * >(node_.get())->getValue(
263 970026 : getComponents());
264 : case Node::KIND_LOCALIZED_PROPERTY:
265 : {
266 29311 : OUString locale(getRootAccess()->getLocale());
267 29311 : if (!Components::allLocales(locale)) {
268 11860 : 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 11860 : return child.is() ? child->asValue() : css::uno::Any();
272 29311 : }
273 : }
274 17451 : break;
275 : case Node::KIND_LOCALIZED_VALUE:
276 23973 : return dynamic_cast< LocalizedValueNode * >(node_.get())->getValue();
277 : default:
278 129855 : break;
279 : }
280 : return css::uno::makeAny(
281 : css::uno::Reference< css::uno::XInterface >(
282 147306 : static_cast< cppu::OWeakObject * >(this)));
283 : }
284 :
285 27271 : void ChildAccess::commitChanges(bool valid, Modifications * globalModifications)
286 : {
287 : assert(globalModifications != 0);
288 27271 : commitChildChanges(valid, globalModifications);
289 27271 : if (valid && changedValue_.get() != 0) {
290 11904 : Path path(getAbsolutePath());
291 11904 : getComponents().addModification(path);
292 11904 : globalModifications->add(path);
293 11904 : switch (node_->kind()) {
294 : case Node::KIND_PROPERTY:
295 11898 : dynamic_cast< PropertyNode * >(node_.get())->setValue(
296 23796 : Data::NO_LAYER, *changedValue_);
297 11898 : break;
298 : case Node::KIND_LOCALIZED_VALUE:
299 6 : dynamic_cast< LocalizedValueNode * >(node_.get())->setValue(
300 12 : Data::NO_LAYER, *changedValue_);
301 6 : break;
302 : default:
303 : assert(false); // this cannot happen
304 0 : break;
305 11904 : }
306 : }
307 27271 : changedValue_.reset();
308 27271 : }
309 :
310 3259635 : ChildAccess::~ChildAccess() {
311 1086545 : osl::MutexGuard g(*lock_);
312 1086545 : if (parent_.is()) {
313 1086477 : parent_->releaseChild(name_);
314 1086545 : }
315 2173090 : }
316 :
317 3 : void ChildAccess::addTypes(std::vector< css::uno::Type > * types) const {
318 : assert(types != 0);
319 3 : types->push_back(cppu::UnoType< css::container::XChild >::get());
320 3 : types->push_back(cppu::UnoType< css::lang::XUnoTunnel >::get());
321 3 : }
322 :
323 264 : void ChildAccess::addSupportedServiceNames(
324 : std::vector< OUString > * services)
325 : {
326 : assert(services != 0);
327 : services->push_back(
328 528 : getParentNode()->kind() == Node::KIND_GROUP
329 : ? OUString("com.sun.star.configuration.GroupElement")
330 264 : : OUString("com.sun.star.configuration.SetElement"));
331 264 : }
332 :
333 153094 : css::uno::Any ChildAccess::queryInterface(css::uno::Type const & aType)
334 : throw (css::uno::RuntimeException)
335 : {
336 : assert(thisIs(IS_ANY));
337 153094 : osl::MutexGuard g(*lock_);
338 153094 : checkLocalizedPropertyAccess();
339 153094 : css::uno::Any res(Access::queryInterface(aType));
340 153094 : return res.hasValue()
341 : ? res
342 : : cppu::queryInterface(
343 : aType, static_cast< css::container::XChild * >(this),
344 153094 : static_cast< css::lang::XUnoTunnel * >(this));
345 : }
346 :
347 : }
348 :
349 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|