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/sequenceasvector.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 comphelper::SequenceAsVector
< css::util::ElementChange
> changes
;
79 initBroadcasterAndChanges(
80 modifications
, broadcaster
, changesListeners_
.empty() ? 0 : &changes
);
81 if (!changes
.empty()) {
82 css::util::ChangesSet
set(changes
.getAsConstList());
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_
;
109 OUString
RootAccess::getLocale() const {
113 bool RootAccess::isUpdate() const {
117 void RootAccess::setAlive(bool b
) {
121 void RootAccess::addChangesListener(
122 css::uno::Reference
< css::util::XChangesListener
> const & aListener
)
123 throw (css::uno::RuntimeException
)
125 assert(thisIs(IS_ANY
));
127 osl::MutexGuard
g(*lock_
);
128 checkLocalizedPropertyAccess();
129 if (!aListener
.is()) {
130 throw css::uno::RuntimeException(
131 OUString("null listener"),
132 static_cast< cppu::OWeakObject
* >(this));
135 changesListeners_
.insert(aListener
);
140 aListener
->disposing(
141 css::lang::EventObject(static_cast< cppu::OWeakObject
* >(this)));
142 } catch (css::lang::DisposedException
&) {}
145 void RootAccess::removeChangesListener(
146 css::uno::Reference
< css::util::XChangesListener
> const & aListener
)
147 throw (css::uno::RuntimeException
)
149 assert(thisIs(IS_ANY
));
150 osl::MutexGuard
g(*lock_
);
151 checkLocalizedPropertyAccess();
152 ChangesListeners::iterator
i(changesListeners_
.find(aListener
));
153 if (i
!= changesListeners_
.end()) {
154 changesListeners_
.erase(i
);
158 void RootAccess::commitChanges()
159 throw (css::lang::WrappedTargetException
, css::uno::RuntimeException
)
161 assert(thisIs(IS_UPDATE
));
168 osl::MutexGuard
g(*lock_
);
170 checkLocalizedPropertyAccess();
172 Modifications globalMods
;
174 ((getComponents().resolvePathRepresentation(
175 pathRepresentation_
, 0, 0, &finalizedLayer
)
177 finalizedLayer
== Data::NO_LAYER
),
179 getComponents().writeModifications();
180 getComponents().initGlobalBroadcaster(globalMods
, this, &bc
);
185 sal_Bool
RootAccess::hasPendingChanges() throw (css::uno::RuntimeException
) {
186 assert(thisIs(IS_UPDATE
));
187 osl::MutexGuard
g(*lock_
);
188 checkLocalizedPropertyAccess();
190 std::vector
< css::util::ElementChange
> changes
;
191 reportChildChanges(&changes
);
192 return !changes
.empty();
195 css::util::ChangesSet
RootAccess::getPendingChanges()
196 throw (css::uno::RuntimeException
)
198 assert(thisIs(IS_UPDATE
));
199 osl::MutexGuard
g(*lock_
);
200 checkLocalizedPropertyAccess();
201 comphelper::SequenceAsVector
< css::util::ElementChange
> changes
;
202 reportChildChanges(&changes
);
203 return changes
.getAsConstList();
206 RootAccess::~RootAccess()
208 osl::MutexGuard
g(*lock_
);
210 getComponents().removeRootAccess(this);
213 Path
RootAccess::getRelativePath() {
217 OUString
RootAccess::getRelativePathRepresentation() {
221 rtl::Reference
< Node
> RootAccess::getNode() {
225 node_
= getComponents().resolvePathRepresentation(
226 pathRepresentation_
, &canonic
, &path_
, &finalizedLayer
);
228 throw css::uno::RuntimeException(
229 (OUString("cannot find ") +
230 pathRepresentation_
),
232 // RootAccess::queryInterface indirectly calls
233 // RootAccess::getNode, so if this RootAccess were passed out in
234 // RuntimeException.Context, client code that called
235 // queryInterface on it would cause trouble; therefore,
236 // RuntimeException.Context is left null here
238 pathRepresentation_
= canonic
;
239 assert(!path_
.empty() || node_
->kind() == Node::KIND_ROOT
);
240 if (!path_
.empty()) {
241 name_
= path_
.back();
243 finalized_
= finalizedLayer
!= Data::NO_LAYER
;
248 bool RootAccess::isFinalized() {
253 OUString
RootAccess::getNameInternal() {
258 rtl::Reference
< RootAccess
> RootAccess::getRootAccess() {
262 rtl::Reference
< Access
> RootAccess::getParentAccess() {
263 return rtl::Reference
< Access
>();
266 void RootAccess::addTypes(std::vector
< css::uno::Type
> * types
) const {
268 types
->push_back(cppu::UnoType
< css::util::XChangesNotifier
>::get());
269 types
->push_back(cppu::UnoType
< css::util::XChangesBatch
>::get());
272 void RootAccess::addSupportedServiceNames(
273 std::vector
< OUString
> * services
)
275 assert(services
!= 0);
277 OUString("com.sun.star.configuration.AccessRootElement"));
280 OUString("com.sun.star.configuration.UpdateRootElement"));
284 void RootAccess::initDisposeBroadcaster(Broadcaster
* broadcaster
) {
285 assert(broadcaster
!= 0);
286 for (ChangesListeners::iterator
i(changesListeners_
.begin());
287 i
!= changesListeners_
.end(); ++i
)
289 broadcaster
->addDisposeNotification(
291 css::lang::EventObject(static_cast< cppu::OWeakObject
* >(this)));
293 Access::initDisposeBroadcaster(broadcaster
);
296 void RootAccess::clearListeners() throw() {
297 changesListeners_
.clear();
298 Access::clearListeners();
301 css::uno::Any
RootAccess::queryInterface(css::uno::Type
const & aType
)
302 throw (css::uno::RuntimeException
)
304 assert(thisIs(IS_ANY
));
305 osl::MutexGuard
g(*lock_
);
306 checkLocalizedPropertyAccess();
307 css::uno::Any
res(Access::queryInterface(aType
));
308 if (res
.hasValue()) {
311 res
= cppu::queryInterface(
312 aType
, static_cast< css::util::XChangesNotifier
* >(this));
313 if (res
.hasValue()) {
316 if (!res
.hasValue() && update_
) {
317 res
= cppu::queryInterface(
318 aType
, static_cast< css::util::XChangesBatch
* >(this));
323 OUString
RootAccess::getImplementationName()
324 throw (css::uno::RuntimeException
)
326 assert(thisIs(IS_ANY
));
327 osl::MutexGuard
g(*lock_
);
328 checkLocalizedPropertyAccess();
329 return OUString("configmgr.RootAccess");
334 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */