bump product version to 5.0.4.1
[LibreOffice.git] / configmgr / source / rootaccess.cxx
blob91967684b94909a60121cb7be8ab93988567f58d
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/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"
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 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(
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_;
111 void RootAccess::setAlive(bool b) {
112 alive_ = 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));
127 if (!isDisposed()) {
128 changesListeners_.insert(aListener);
129 return;
132 try {
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,
154 std::exception)
156 assert(thisIs(IS_UPDATE));
157 if (!alive_)
159 return;
161 Broadcaster bc;
163 osl::MutexGuard g(*lock_);
165 checkLocalizedPropertyAccess();
166 int finalizedLayer;
167 Modifications globalMods;
168 commitChildChanges(
169 ((getComponents().resolvePathRepresentation(
170 pathRepresentation_, 0, 0, &finalizedLayer)
171 == node_) &&
172 finalizedLayer == Data::NO_LAYER),
173 &globalMods);
174 getComponents().writeModifications();
175 getComponents().initGlobalBroadcaster(globalMods, this, &bc);
177 bc.send();
180 sal_Bool RootAccess::hasPendingChanges() throw (css::uno::RuntimeException, std::exception) {
181 assert(thisIs(IS_UPDATE));
182 osl::MutexGuard g(*lock_);
183 checkLocalizedPropertyAccess();
184 //TODO: Optimize:
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_);
204 if (alive_)
205 getComponents().removeRootAccess(this);
208 Path RootAccess::getRelativePath() {
209 return Path();
212 OUString RootAccess::getRelativePathRepresentation() {
213 return OUString();
216 rtl::Reference< Node > RootAccess::getNode() {
217 if (!node_.is()) {
218 OUString canonic;
219 int finalizedLayer;
220 node_ = getComponents().resolvePathRepresentation(
221 pathRepresentation_, &canonic, &path_, &finalizedLayer);
222 if (!node_.is()) {
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;
238 return node_;
241 bool RootAccess::isFinalized() {
242 getNode();
243 return finalized_;
246 OUString RootAccess::getNameInternal() {
247 getNode();
248 return name_;
251 rtl::Reference< RootAccess > RootAccess::getRootAccess() {
252 return this;
255 rtl::Reference< Access > RootAccess::getParentAccess() {
256 return rtl::Reference< Access >();
259 void RootAccess::addTypes(std::vector< css::uno::Type > * types) const {
260 assert(types != 0);
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");
270 if (update_) {
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(
281 i->get(),
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()) {
300 return res;
302 res = cppu::queryInterface(
303 aType, static_cast< css::util::XChangesNotifier * >(this));
304 if (res.hasValue()) {
305 return res;
307 if (!res.hasValue() && update_) {
308 res = cppu::queryInterface(
309 aType, static_cast< css::util::XChangesBatch * >(this));
311 return res;
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: */