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/lang/WrappedTargetException.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.h>
46 #include <rtl/ustring.hxx>
48 #include "broadcaster.hxx"
49 #include "childaccess.hxx"
50 #include "components.hxx"
53 #include "modifications.hxx"
55 #include "rootaccess.hxx"
59 RootAccess::RootAccess(
60 Components
& components
, OUString
const & pathRepresentation
,
61 OUString
const & locale
, bool update
):
62 Access(components
), pathRepresentation_(pathRepresentation
),
65 update_(update
), finalized_(false), alive_(true)
69 std::vector
<OUString
> RootAccess::getAbsolutePath() {
74 void RootAccess::initBroadcaster(
75 Modifications::Node
const & modifications
, Broadcaster
* broadcaster
)
77 assert(broadcaster
!= nullptr);
78 std::vector
< css::util::ElementChange
> changes
;
79 initBroadcasterAndChanges(
80 modifications
, broadcaster
, changesListeners_
.empty() ? nullptr : &changes
);
81 if (!changes
.empty()) {
82 css::util::ChangesSet
set(comphelper::containerToSequence(changes
));
83 for (auto const& changesListener
: changesListeners_
)
85 cppu::OWeakObject
* pSource
= static_cast< cppu::OWeakObject
* >(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
));
95 void RootAccess::acquire() throw () {
99 void RootAccess::release() throw () {
103 OUString
const & RootAccess::getAbsolutePathRepresentation() {
104 getNode(); // turn pathRepresentation_ into canonic form
105 return pathRepresentation_
;
109 void RootAccess::setAlive(bool b
) {
113 void RootAccess::addChangesListener(
114 css::uno::Reference
< css::util::XChangesListener
> const & aListener
)
116 assert(thisIs(IS_ANY
));
118 osl::MutexGuard
g(*lock_
);
119 checkLocalizedPropertyAccess();
120 if (!aListener
.is()) {
121 throw css::uno::RuntimeException(
122 "null listener", static_cast< cppu::OWeakObject
* >(this));
125 changesListeners_
.insert(aListener
);
130 aListener
->disposing(
131 css::lang::EventObject(static_cast< cppu::OWeakObject
* >(this)));
132 } catch (css::lang::DisposedException
&) {}
135 void RootAccess::removeChangesListener(
136 css::uno::Reference
< css::util::XChangesListener
> const & aListener
)
138 assert(thisIs(IS_ANY
));
139 osl::MutexGuard
g(*lock_
);
140 checkLocalizedPropertyAccess();
141 ChangesListeners::iterator
i(changesListeners_
.find(aListener
));
142 if (i
!= changesListeners_
.end()) {
143 changesListeners_
.erase(i
);
147 void RootAccess::commitChanges()
149 assert(thisIs(IS_UPDATE
));
156 osl::MutexGuard
g(*lock_
);
158 checkLocalizedPropertyAccess();
160 Modifications globalMods
;
162 ((getComponents().resolvePathRepresentation(
163 pathRepresentation_
, nullptr, nullptr, &finalizedLayer
)
165 finalizedLayer
== Data::NO_LAYER
),
167 getComponents().writeModifications();
168 getComponents().initGlobalBroadcaster(globalMods
, this, &bc
);
173 sal_Bool
RootAccess::hasPendingChanges() {
174 assert(thisIs(IS_UPDATE
));
175 osl::MutexGuard
g(*lock_
);
176 checkLocalizedPropertyAccess();
178 std::vector
< css::util::ElementChange
> changes
;
179 reportChildChanges(&changes
);
180 return !changes
.empty();
183 css::uno::Sequence
< ::css::util::ElementChange
> RootAccess::getPendingChanges()
185 assert(thisIs(IS_UPDATE
));
186 osl::MutexGuard
g(*lock_
);
187 checkLocalizedPropertyAccess();
188 std::vector
< css::util::ElementChange
> changes
;
189 reportChildChanges(&changes
);
190 return comphelper::containerToSequence(changes
);
193 RootAccess::~RootAccess()
195 osl::MutexGuard
g(*lock_
);
197 getComponents().removeRootAccess(this);
200 std::vector
<OUString
> RootAccess::getRelativePath() {
201 return std::vector
<OUString
>();
204 OUString
RootAccess::getRelativePathRepresentation() {
208 rtl::Reference
< Node
> RootAccess::getNode() {
212 node_
= getComponents().resolvePathRepresentation(
213 pathRepresentation_
, &canonic
, &path_
, &finalizedLayer
);
215 throw css::uno::RuntimeException(
216 "cannot find " + pathRepresentation_
, nullptr);
217 // RootAccess::queryInterface indirectly calls
218 // RootAccess::getNode, so if this RootAccess were passed out in
219 // RuntimeException.Context, client code that called
220 // queryInterface on it would cause trouble; therefore,
221 // RuntimeException.Context is left null here
223 pathRepresentation_
= canonic
;
224 assert(!path_
.empty() || node_
->kind() == Node::KIND_ROOT
);
225 if (!path_
.empty()) {
226 name_
= path_
.back();
228 finalized_
= finalizedLayer
!= Data::NO_LAYER
;
233 bool RootAccess::isFinalized() {
238 OUString
RootAccess::getNameInternal() {
243 rtl::Reference
< RootAccess
> RootAccess::getRootAccess() {
247 rtl::Reference
< Access
> RootAccess::getParentAccess() {
248 return rtl::Reference
< Access
>();
251 void RootAccess::addTypes(std::vector
< css::uno::Type
> * types
) const {
252 assert(types
!= nullptr);
253 types
->push_back(cppu::UnoType
< css::util::XChangesNotifier
>::get());
254 types
->push_back(cppu::UnoType
< css::util::XChangesBatch
>::get());
257 void RootAccess::addSupportedServiceNames(
258 std::vector
<OUString
> * services
)
260 assert(services
!= nullptr);
261 services
->push_back("com.sun.star.configuration.AccessRootElement");
263 services
->push_back("com.sun.star.configuration.UpdateRootElement");
267 void RootAccess::initDisposeBroadcaster(Broadcaster
* broadcaster
) {
268 assert(broadcaster
!= nullptr);
269 for (auto const& changesListener
: changesListeners_
)
271 broadcaster
->addDisposeNotification(
272 changesListener
.get(),
273 css::lang::EventObject(static_cast< cppu::OWeakObject
* >(this)));
275 Access::initDisposeBroadcaster(broadcaster
);
278 void RootAccess::clearListeners() throw() {
279 changesListeners_
.clear();
280 Access::clearListeners();
283 css::uno::Any
RootAccess::queryInterface(css::uno::Type
const & aType
)
285 assert(thisIs(IS_ANY
));
286 osl::MutexGuard
g(*lock_
);
287 checkLocalizedPropertyAccess();
288 css::uno::Any
res(Access::queryInterface(aType
));
289 if (res
.hasValue()) {
292 res
= cppu::queryInterface(
293 aType
, static_cast< css::util::XChangesNotifier
* >(this));
294 if (res
.hasValue()) {
297 if (!res
.hasValue() && update_
) {
298 res
= cppu::queryInterface(
299 aType
, static_cast< css::util::XChangesBatch
* >(this));
304 OUString
RootAccess::getImplementationName()
306 assert(thisIs(IS_ANY
));
307 osl::MutexGuard
g(*lock_
);
308 checkLocalizedPropertyAccess();
309 return OUString("configmgr.RootAccess");
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */