1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
26 #include <com/sun/star/lang/DisposedException.hpp>
27 #include <com/sun/star/lang/EventObject.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/Type.hxx>
32 #include <com/sun/star/uno/XInterface.hpp>
33 #include <com/sun/star/util/ChangesEvent.hpp>
34 #include <com/sun/star/util/ChangesSet.hpp>
35 #include <com/sun/star/util/ElementChange.hpp>
36 #include <com/sun/star/util/XChangesBatch.hpp>
37 #include <com/sun/star/util/XChangesListener.hpp>
38 #include <com/sun/star/util/XChangesNotifier.hpp>
39 #include <comphelper/sequence.hxx>
40 #include <cppu/unotype.hxx>
41 #include <cppuhelper/queryinterface.hxx>
42 #include <cppuhelper/weak.hxx>
43 #include <osl/mutex.hxx>
44 #include <rtl/ref.hxx>
45 #include <rtl/ustring.hxx>
47 #include "broadcaster.hxx"
48 #include "components.hxx"
51 #include "modifications.hxx"
53 #include "rootaccess.hxx"
57 RootAccess::RootAccess(
58 Components
& components
, OUString pathRepresentation
,
59 OUString locale
, bool update
):
60 Access(components
), pathRepresentation_(std::move(pathRepresentation
)),
61 locale_(std::move(locale
)),
63 update_(update
), finalized_(false), alive_(true)
67 std::vector
<OUString
> RootAccess::getAbsolutePath() {
72 void RootAccess::initBroadcaster(
73 Modifications::Node
const & modifications
, Broadcaster
* broadcaster
)
75 assert(broadcaster
!= nullptr);
76 std::vector
< css::util::ElementChange
> changes
;
77 initBroadcasterAndChanges(
78 modifications
, broadcaster
, changesListeners_
.empty() ? nullptr : &changes
);
82 css::util::ChangesSet
set(comphelper::containerToSequence(changes
));
83 for (auto const& changesListener
: changesListeners_
)
85 cppu::OWeakObject
* pSource
= this;
86 css::uno::Reference
< css::uno::XInterface
> xBase( pSource
, css::uno::UNO_QUERY
);
87 broadcaster
->addChangesNotification(
89 css::util::ChangesEvent(
90 pSource
, css::uno::Any( xBase
), set
), path_
.empty());
94 void RootAccess::acquire() noexcept
{
98 void RootAccess::release() noexcept
{
102 OUString
const & RootAccess::getAbsolutePathRepresentation() {
103 getNode(); // turn pathRepresentation_ into canonic form
104 return pathRepresentation_
;
108 void RootAccess::setAlive(bool b
) {
112 void RootAccess::addChangesListener(
113 css::uno::Reference
< css::util::XChangesListener
> const & aListener
)
115 assert(thisIs(IS_ANY
));
117 osl::MutexGuard
g(*lock_
);
118 checkLocalizedPropertyAccess();
119 if (!aListener
.is()) {
120 throw css::uno::RuntimeException(
121 "null listener", getXWeak());
124 changesListeners_
.insert(aListener
);
129 aListener
->disposing(
130 css::lang::EventObject(getXWeak()));
131 } catch (css::lang::DisposedException
&) {}
134 void RootAccess::removeChangesListener(
135 css::uno::Reference
< css::util::XChangesListener
> const & aListener
)
137 assert(thisIs(IS_ANY
));
138 osl::MutexGuard
g(*lock_
);
139 checkLocalizedPropertyAccess();
140 ChangesListeners::iterator
i(changesListeners_
.find(aListener
));
141 if (i
!= changesListeners_
.end()) {
142 changesListeners_
.erase(i
);
146 void RootAccess::commitChanges()
148 assert(thisIs(IS_UPDATE
));
155 osl::MutexGuard
g(*lock_
);
157 checkLocalizedPropertyAccess();
159 Modifications globalMods
;
161 ((getComponents().resolvePathRepresentation(
162 pathRepresentation_
, nullptr, nullptr, &finalizedLayer
)
164 finalizedLayer
== Data::NO_LAYER
),
166 getComponents().writeModifications();
167 getComponents().initGlobalBroadcaster(globalMods
, this, &bc
);
172 sal_Bool
RootAccess::hasPendingChanges() {
173 assert(thisIs(IS_UPDATE
));
174 osl::MutexGuard
g(*lock_
);
175 checkLocalizedPropertyAccess();
177 std::vector
< css::util::ElementChange
> changes
;
178 reportChildChanges(&changes
);
179 return !changes
.empty();
182 css::uno::Sequence
< ::css::util::ElementChange
> RootAccess::getPendingChanges()
184 assert(thisIs(IS_UPDATE
));
185 osl::MutexGuard
g(*lock_
);
186 checkLocalizedPropertyAccess();
187 std::vector
< css::util::ElementChange
> changes
;
188 reportChildChanges(&changes
);
189 return comphelper::containerToSequence(changes
);
192 RootAccess::~RootAccess()
194 osl::MutexGuard
g(*lock_
);
196 getComponents().removeRootAccess(this);
199 std::vector
<OUString
> RootAccess::getRelativePath() {
200 return std::vector
<OUString
>();
203 OUString
RootAccess::getRelativePathRepresentation() {
207 const rtl::Reference
< Node
> & RootAccess::getNode() {
211 node_
= getComponents().resolvePathRepresentation(
212 pathRepresentation_
, &canonic
, &path_
, &finalizedLayer
);
214 throw css::uno::RuntimeException(
215 "cannot find " + pathRepresentation_
, nullptr);
216 // RootAccess::queryInterface indirectly calls
217 // RootAccess::getNode, so if this RootAccess were passed out in
218 // RuntimeException.Context, client code that called
219 // queryInterface on it would cause trouble; therefore,
220 // RuntimeException.Context is left null here
222 pathRepresentation_
= canonic
;
223 assert(!path_
.empty() || node_
->kind() == Node::KIND_ROOT
);
224 if (!path_
.empty()) {
225 name_
= path_
.back();
227 finalized_
= finalizedLayer
!= Data::NO_LAYER
;
232 bool RootAccess::isFinalized() {
237 const OUString
& RootAccess::getNameInternal() {
242 rtl::Reference
< RootAccess
> RootAccess::getRootAccess() {
246 rtl::Reference
< Access
> RootAccess::getParentAccess() {
247 return rtl::Reference
< Access
>();
250 void RootAccess::addTypes(std::vector
< css::uno::Type
> * types
) const {
251 assert(types
!= nullptr);
252 types
->push_back(cppu::UnoType
< css::util::XChangesNotifier
>::get());
253 types
->push_back(cppu::UnoType
< css::util::XChangesBatch
>::get());
256 void RootAccess::addSupportedServiceNames(
257 std::vector
<OUString
> * services
)
259 assert(services
!= nullptr);
260 services
->push_back("com.sun.star.configuration.AccessRootElement");
262 services
->push_back("com.sun.star.configuration.UpdateRootElement");
266 void RootAccess::initDisposeBroadcaster(Broadcaster
* broadcaster
) {
267 assert(broadcaster
!= nullptr);
268 for (auto const& changesListener
: changesListeners_
)
270 broadcaster
->addDisposeNotification(
272 css::lang::EventObject(getXWeak()));
274 Access::initDisposeBroadcaster(broadcaster
);
277 void RootAccess::clearListeners() noexcept
{
278 changesListeners_
.clear();
279 Access::clearListeners();
282 css::uno::Any
RootAccess::queryInterface(css::uno::Type
const & aType
)
284 assert(thisIs(IS_ANY
));
285 osl::MutexGuard
g(*lock_
);
286 checkLocalizedPropertyAccess();
287 css::uno::Any
res(Access::queryInterface(aType
));
288 if (res
.hasValue()) {
291 res
= cppu::queryInterface(
292 aType
, static_cast< css::util::XChangesNotifier
* >(this));
293 if (res
.hasValue()) {
296 if (!res
.hasValue() && update_
) {
297 res
= cppu::queryInterface(
298 aType
, static_cast< css::util::XChangesBatch
* >(this));
303 OUString
RootAccess::getImplementationName()
305 assert(thisIs(IS_ANY
));
306 osl::MutexGuard
g(*lock_
);
307 checkLocalizedPropertyAccess();
308 return "configmgr.RootAccess";
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */