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"
56 #include "rootaccess.hxx"
60 RootAccess::RootAccess(
61 Components
& components
, OUString
const & pathRepresentation
,
62 OUString
const & locale
, bool update
):
63 Access(components
), pathRepresentation_(pathRepresentation
),
64 locale_(locale
), update_(update
), finalized_(false), alive_(true)
69 Path
RootAccess::getAbsolutePath() {
74 void RootAccess::initBroadcaster(
75 Modifications::Node
const & modifications
, Broadcaster
* broadcaster
)
77 assert(broadcaster
!= 0);
78 std::vector
< css::util::ElementChange
> changes
;
79 initBroadcasterAndChanges(
80 modifications
, broadcaster
, changesListeners_
.empty() ? 0 : &changes
);
81 if (!changes
.empty()) {
82 css::util::ChangesSet
set(comphelper::containerToSequence(changes
));
83 for (ChangesListeners::iterator
i(changesListeners_
.begin());
84 i
!= changesListeners_
.end(); ++i
)
86 cppu::OWeakObject
* pSource
= static_cast< cppu::OWeakObject
* >(this);
87 css::uno::Reference
< css::uno::XInterface
> xBase( pSource
, css::uno::UNO_QUERY
);
88 broadcaster
->addChangesNotification(
90 css::util::ChangesEvent(
91 pSource
, makeAny( xBase
), set
));
96 void RootAccess::acquire() throw () {
100 void RootAccess::release() throw () {
104 OUString
RootAccess::getAbsolutePathRepresentation() {
105 getNode(); // turn pathRepresentation_ into canonic form
106 return pathRepresentation_
;
111 void RootAccess::setAlive(bool b
) {
115 void RootAccess::addChangesListener(
116 css::uno::Reference
< css::util::XChangesListener
> const & aListener
)
117 throw (css::uno::RuntimeException
, std::exception
)
119 assert(thisIs(IS_ANY
));
121 osl::MutexGuard
g(*lock_
);
122 checkLocalizedPropertyAccess();
123 if (!aListener
.is()) {
124 throw css::uno::RuntimeException(
125 "null listener", static_cast< cppu::OWeakObject
* >(this));
128 changesListeners_
.insert(aListener
);
133 aListener
->disposing(
134 css::lang::EventObject(static_cast< cppu::OWeakObject
* >(this)));
135 } catch (css::lang::DisposedException
&) {}
138 void RootAccess::removeChangesListener(
139 css::uno::Reference
< css::util::XChangesListener
> const & aListener
)
140 throw (css::uno::RuntimeException
, std::exception
)
142 assert(thisIs(IS_ANY
));
143 osl::MutexGuard
g(*lock_
);
144 checkLocalizedPropertyAccess();
145 ChangesListeners::iterator
i(changesListeners_
.find(aListener
));
146 if (i
!= changesListeners_
.end()) {
147 changesListeners_
.erase(i
);
151 void RootAccess::commitChanges()
152 throw (css::lang::WrappedTargetException
,
153 css::uno::RuntimeException
,
156 assert(thisIs(IS_UPDATE
));
163 osl::MutexGuard
g(*lock_
);
165 checkLocalizedPropertyAccess();
167 Modifications globalMods
;
169 ((getComponents().resolvePathRepresentation(
170 pathRepresentation_
, 0, 0, &finalizedLayer
)
172 finalizedLayer
== Data::NO_LAYER
),
174 getComponents().writeModifications();
175 getComponents().initGlobalBroadcaster(globalMods
, this, &bc
);
180 sal_Bool
RootAccess::hasPendingChanges() throw (css::uno::RuntimeException
, std::exception
) {
181 assert(thisIs(IS_UPDATE
));
182 osl::MutexGuard
g(*lock_
);
183 checkLocalizedPropertyAccess();
185 std::vector
< css::util::ElementChange
> changes
;
186 reportChildChanges(&changes
);
187 return !changes
.empty();
190 css::util::ChangesSet
RootAccess::getPendingChanges()
191 throw (css::uno::RuntimeException
, std::exception
)
193 assert(thisIs(IS_UPDATE
));
194 osl::MutexGuard
g(*lock_
);
195 checkLocalizedPropertyAccess();
196 std::vector
< css::util::ElementChange
> changes
;
197 reportChildChanges(&changes
);
198 return comphelper::containerToSequence(changes
);
201 RootAccess::~RootAccess()
203 osl::MutexGuard
g(*lock_
);
205 getComponents().removeRootAccess(this);
208 Path
RootAccess::getRelativePath() {
212 OUString
RootAccess::getRelativePathRepresentation() {
216 rtl::Reference
< Node
> RootAccess::getNode() {
220 node_
= getComponents().resolvePathRepresentation(
221 pathRepresentation_
, &canonic
, &path_
, &finalizedLayer
);
223 throw css::uno::RuntimeException(
224 "cannot find " + pathRepresentation_
, 0);
225 // RootAccess::queryInterface indirectly calls
226 // RootAccess::getNode, so if this RootAccess were passed out in
227 // RuntimeException.Context, client code that called
228 // queryInterface on it would cause trouble; therefore,
229 // RuntimeException.Context is left null here
231 pathRepresentation_
= canonic
;
232 assert(!path_
.empty() || node_
->kind() == Node::KIND_ROOT
);
233 if (!path_
.empty()) {
234 name_
= path_
.back();
236 finalized_
= finalizedLayer
!= Data::NO_LAYER
;
241 bool RootAccess::isFinalized() {
246 OUString
RootAccess::getNameInternal() {
251 rtl::Reference
< RootAccess
> RootAccess::getRootAccess() {
255 rtl::Reference
< Access
> RootAccess::getParentAccess() {
256 return rtl::Reference
< Access
>();
259 void RootAccess::addTypes(std::vector
< css::uno::Type
> * types
) const {
261 types
->push_back(cppu::UnoType
< css::util::XChangesNotifier
>::get());
262 types
->push_back(cppu::UnoType
< css::util::XChangesBatch
>::get());
265 void RootAccess::addSupportedServiceNames(
266 std::vector
< OUString
> * services
)
268 assert(services
!= 0);
269 services
->push_back("com.sun.star.configuration.AccessRootElement");
271 services
->push_back("com.sun.star.configuration.UpdateRootElement");
275 void RootAccess::initDisposeBroadcaster(Broadcaster
* broadcaster
) {
276 assert(broadcaster
!= 0);
277 for (ChangesListeners::iterator
i(changesListeners_
.begin());
278 i
!= changesListeners_
.end(); ++i
)
280 broadcaster
->addDisposeNotification(
282 css::lang::EventObject(static_cast< cppu::OWeakObject
* >(this)));
284 Access::initDisposeBroadcaster(broadcaster
);
287 void RootAccess::clearListeners() throw() {
288 changesListeners_
.clear();
289 Access::clearListeners();
292 css::uno::Any
RootAccess::queryInterface(css::uno::Type
const & aType
)
293 throw (css::uno::RuntimeException
, std::exception
)
295 assert(thisIs(IS_ANY
));
296 osl::MutexGuard
g(*lock_
);
297 checkLocalizedPropertyAccess();
298 css::uno::Any
res(Access::queryInterface(aType
));
299 if (res
.hasValue()) {
302 res
= cppu::queryInterface(
303 aType
, static_cast< css::util::XChangesNotifier
* >(this));
304 if (res
.hasValue()) {
307 if (!res
.hasValue() && update_
) {
308 res
= cppu::queryInterface(
309 aType
, static_cast< css::util::XChangesBatch
* >(this));
314 OUString
RootAccess::getImplementationName()
315 throw (css::uno::RuntimeException
, std::exception
)
317 assert(thisIs(IS_ANY
));
318 osl::MutexGuard
g(*lock_
);
319 checkLocalizedPropertyAccess();
320 return OUString("configmgr.RootAccess");
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */