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>
25 #include <com/sun/star/lang/DisposedException.hpp>
26 #include <com/sun/star/lang/EventObject.hpp>
27 #include <com/sun/star/uno/Any.hxx>
28 #include <com/sun/star/uno/Reference.hxx>
29 #include <com/sun/star/uno/RuntimeException.hpp>
30 #include <com/sun/star/uno/Type.hxx>
31 #include <com/sun/star/uno/XInterface.hpp>
32 #include <com/sun/star/util/ChangesEvent.hpp>
33 #include <com/sun/star/util/ChangesSet.hpp>
34 #include <com/sun/star/util/ElementChange.hpp>
35 #include <com/sun/star/util/XChangesBatch.hpp>
36 #include <com/sun/star/util/XChangesListener.hpp>
37 #include <com/sun/star/util/XChangesNotifier.hpp>
38 #include <comphelper/sequence.hxx>
39 #include <cppu/unotype.hxx>
40 #include <cppuhelper/queryinterface.hxx>
41 #include <cppuhelper/weak.hxx>
42 #include <osl/mutex.hxx>
43 #include <rtl/ref.hxx>
44 #include <rtl/ustring.hxx>
46 #include "broadcaster.hxx"
47 #include "components.hxx"
50 #include "modifications.hxx"
52 #include "rootaccess.hxx"
56 RootAccess::RootAccess(
57 Components
& components
, OUString
const & pathRepresentation
,
58 OUString
const & locale
, bool update
):
59 Access(components
), pathRepresentation_(pathRepresentation
),
62 update_(update
), finalized_(false), alive_(true)
66 std::vector
<OUString
> RootAccess::getAbsolutePath() {
71 void RootAccess::initBroadcaster(
72 Modifications::Node
const & modifications
, Broadcaster
* broadcaster
)
74 assert(broadcaster
!= nullptr);
75 std::vector
< css::util::ElementChange
> changes
;
76 initBroadcasterAndChanges(
77 modifications
, broadcaster
, changesListeners_
.empty() ? nullptr : &changes
);
81 css::util::ChangesSet
set(comphelper::containerToSequence(changes
));
82 for (auto const& changesListener
: changesListeners_
)
84 cppu::OWeakObject
* pSource
= this;
85 css::uno::Reference
< css::uno::XInterface
> xBase( pSource
, css::uno::UNO_QUERY
);
86 broadcaster
->addChangesNotification(
88 css::util::ChangesEvent(
89 pSource
, css::uno::Any( xBase
), set
));
93 void RootAccess::acquire() noexcept
{
97 void RootAccess::release() noexcept
{
101 OUString
const & RootAccess::getAbsolutePathRepresentation() {
102 getNode(); // turn pathRepresentation_ into canonic form
103 return pathRepresentation_
;
107 void RootAccess::setAlive(bool b
) {
111 void RootAccess::addChangesListener(
112 css::uno::Reference
< css::util::XChangesListener
> const & aListener
)
114 assert(thisIs(IS_ANY
));
116 osl::MutexGuard
g(*lock_
);
117 checkLocalizedPropertyAccess();
118 if (!aListener
.is()) {
119 throw css::uno::RuntimeException(
120 "null listener", static_cast< cppu::OWeakObject
* >(this));
123 changesListeners_
.insert(aListener
);
128 aListener
->disposing(
129 css::lang::EventObject(static_cast< cppu::OWeakObject
* >(this)));
130 } catch (css::lang::DisposedException
&) {}
133 void RootAccess::removeChangesListener(
134 css::uno::Reference
< css::util::XChangesListener
> const & aListener
)
136 assert(thisIs(IS_ANY
));
137 osl::MutexGuard
g(*lock_
);
138 checkLocalizedPropertyAccess();
139 ChangesListeners::iterator
i(changesListeners_
.find(aListener
));
140 if (i
!= changesListeners_
.end()) {
141 changesListeners_
.erase(i
);
145 void RootAccess::commitChanges()
147 assert(thisIs(IS_UPDATE
));
154 osl::MutexGuard
g(*lock_
);
156 checkLocalizedPropertyAccess();
158 Modifications globalMods
;
160 ((getComponents().resolvePathRepresentation(
161 pathRepresentation_
, nullptr, nullptr, &finalizedLayer
)
163 finalizedLayer
== Data::NO_LAYER
),
165 getComponents().writeModifications();
166 getComponents().initGlobalBroadcaster(globalMods
, this, &bc
);
171 sal_Bool
RootAccess::hasPendingChanges() {
172 assert(thisIs(IS_UPDATE
));
173 osl::MutexGuard
g(*lock_
);
174 checkLocalizedPropertyAccess();
176 std::vector
< css::util::ElementChange
> changes
;
177 reportChildChanges(&changes
);
178 return !changes
.empty();
181 css::uno::Sequence
< ::css::util::ElementChange
> RootAccess::getPendingChanges()
183 assert(thisIs(IS_UPDATE
));
184 osl::MutexGuard
g(*lock_
);
185 checkLocalizedPropertyAccess();
186 std::vector
< css::util::ElementChange
> changes
;
187 reportChildChanges(&changes
);
188 return comphelper::containerToSequence(changes
);
191 RootAccess::~RootAccess()
193 osl::MutexGuard
g(*lock_
);
195 getComponents().removeRootAccess(this);
198 std::vector
<OUString
> RootAccess::getRelativePath() {
199 return std::vector
<OUString
>();
202 OUString
RootAccess::getRelativePathRepresentation() {
206 rtl::Reference
< Node
> RootAccess::getNode() {
210 node_
= getComponents().resolvePathRepresentation(
211 pathRepresentation_
, &canonic
, &path_
, &finalizedLayer
);
213 throw css::uno::RuntimeException(
214 "cannot find " + pathRepresentation_
, nullptr);
215 // RootAccess::queryInterface indirectly calls
216 // RootAccess::getNode, so if this RootAccess were passed out in
217 // RuntimeException.Context, client code that called
218 // queryInterface on it would cause trouble; therefore,
219 // RuntimeException.Context is left null here
221 pathRepresentation_
= canonic
;
222 assert(!path_
.empty() || node_
->kind() == Node::KIND_ROOT
);
223 if (!path_
.empty()) {
224 name_
= path_
.back();
226 finalized_
= finalizedLayer
!= Data::NO_LAYER
;
231 bool RootAccess::isFinalized() {
236 OUString
RootAccess::getNameInternal() {
241 rtl::Reference
< RootAccess
> RootAccess::getRootAccess() {
245 rtl::Reference
< Access
> RootAccess::getParentAccess() {
246 return rtl::Reference
< Access
>();
249 void RootAccess::addTypes(std::vector
< css::uno::Type
> * types
) const {
250 assert(types
!= nullptr);
251 types
->push_back(cppu::UnoType
< css::util::XChangesNotifier
>::get());
252 types
->push_back(cppu::UnoType
< css::util::XChangesBatch
>::get());
255 void RootAccess::addSupportedServiceNames(
256 std::vector
<OUString
> * services
)
258 assert(services
!= nullptr);
259 services
->push_back("com.sun.star.configuration.AccessRootElement");
261 services
->push_back("com.sun.star.configuration.UpdateRootElement");
265 void RootAccess::initDisposeBroadcaster(Broadcaster
* broadcaster
) {
266 assert(broadcaster
!= nullptr);
267 for (auto const& changesListener
: changesListeners_
)
269 broadcaster
->addDisposeNotification(
271 css::lang::EventObject(static_cast< cppu::OWeakObject
* >(this)));
273 Access::initDisposeBroadcaster(broadcaster
);
276 void RootAccess::clearListeners() noexcept
{
277 changesListeners_
.clear();
278 Access::clearListeners();
281 css::uno::Any
RootAccess::queryInterface(css::uno::Type
const & aType
)
283 assert(thisIs(IS_ANY
));
284 osl::MutexGuard
g(*lock_
);
285 checkLocalizedPropertyAccess();
286 css::uno::Any
res(Access::queryInterface(aType
));
287 if (res
.hasValue()) {
290 res
= cppu::queryInterface(
291 aType
, static_cast< css::util::XChangesNotifier
* >(this));
292 if (res
.hasValue()) {
295 if (!res
.hasValue() && update_
) {
296 res
= cppu::queryInterface(
297 aType
, static_cast< css::util::XChangesBatch
* >(this));
302 OUString
RootAccess::getImplementationName()
304 assert(thisIs(IS_ANY
));
305 osl::MutexGuard
g(*lock_
);
306 checkLocalizedPropertyAccess();
307 return "configmgr.RootAccess";
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */