bump product version to 4.1.6.2
[LibreOffice.git] / configmgr / source / rootaccess.cxx
blob06c0d77f7fc42fc43ba88d28802d20db11f0be80
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
22 #include <cassert>
23 #include <vector>
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"
51 #include "data.hxx"
52 #include "lock.hxx"
53 #include "modifications.hxx"
54 #include "node.hxx"
55 #include "path.hxx"
56 #include "rootaccess.hxx"
58 namespace configmgr {
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)
66 lock_ = lock();
69 Path RootAccess::getAbsolutePath() {
70 getNode();
71 return path_;
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(
89 *i,
90 css::util::ChangesEvent(
91 pSource, makeAny( xBase ), set));
96 void RootAccess::acquire() throw () {
97 Access::acquire();
100 void RootAccess::release() throw () {
101 Access::release();
104 OUString RootAccess::getAbsolutePathRepresentation() {
105 getNode(); // turn pathRepresentation_ into canonic form
106 return pathRepresentation_;
109 OUString RootAccess::getLocale() const {
110 return locale_;
113 bool RootAccess::isUpdate() const {
114 return update_;
117 void RootAccess::setAlive(bool b) {
118 alive_ = 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));
134 if (!isDisposed()) {
135 changesListeners_.insert(aListener);
136 return;
139 try {
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));
162 if (!alive_)
164 return;
166 Broadcaster bc;
168 osl::MutexGuard g(*lock_);
170 checkLocalizedPropertyAccess();
171 int finalizedLayer;
172 Modifications globalMods;
173 commitChildChanges(
174 ((getComponents().resolvePathRepresentation(
175 pathRepresentation_, 0, 0, &finalizedLayer)
176 == node_) &&
177 finalizedLayer == Data::NO_LAYER),
178 &globalMods);
179 getComponents().writeModifications();
180 getComponents().initGlobalBroadcaster(globalMods, this, &bc);
182 bc.send();
185 sal_Bool RootAccess::hasPendingChanges() throw (css::uno::RuntimeException) {
186 assert(thisIs(IS_UPDATE));
187 osl::MutexGuard g(*lock_);
188 checkLocalizedPropertyAccess();
189 //TODO: Optimize:
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_);
209 if (alive_)
210 getComponents().removeRootAccess(this);
213 Path RootAccess::getRelativePath() {
214 return Path();
217 OUString RootAccess::getRelativePathRepresentation() {
218 return OUString();
221 rtl::Reference< Node > RootAccess::getNode() {
222 if (!node_.is()) {
223 OUString canonic;
224 int finalizedLayer;
225 node_ = getComponents().resolvePathRepresentation(
226 pathRepresentation_, &canonic, &path_, &finalizedLayer);
227 if (!node_.is()) {
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;
245 return node_;
248 bool RootAccess::isFinalized() {
249 getNode();
250 return finalized_;
253 OUString RootAccess::getNameInternal() {
254 getNode();
255 return name_;
258 rtl::Reference< RootAccess > RootAccess::getRootAccess() {
259 return this;
262 rtl::Reference< Access > RootAccess::getParentAccess() {
263 return rtl::Reference< Access >();
266 void RootAccess::addTypes(std::vector< css::uno::Type > * types) const {
267 assert(types != 0);
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);
276 services->push_back(
277 OUString("com.sun.star.configuration.AccessRootElement"));
278 if (update_) {
279 services->push_back(
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(
290 i->get(),
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()) {
309 return res;
311 res = cppu::queryInterface(
312 aType, static_cast< css::util::XChangesNotifier * >(this));
313 if (res.hasValue()) {
314 return res;
316 if (!res.hasValue() && update_) {
317 res = cppu::queryInterface(
318 aType, static_cast< css::util::XChangesBatch * >(this));
320 return res;
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: */