Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / configmgr / source / rootaccess.cxx
blob17c9f111a27a58cdb0c0257e2a22e9348d0e16e3
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/uno/Any.hxx>
28 #include <com/sun/star/uno/Reference.hxx>
29 #include <com/sun/star/uno/RuntimeException.hpp>
30 #include <com/sun/star/uno/Type.hxx>
31 #include <com/sun/star/uno/XInterface.hpp>
32 #include <com/sun/star/util/ChangesEvent.hpp>
33 #include <com/sun/star/util/ChangesSet.hpp>
34 #include <com/sun/star/util/ElementChange.hpp>
35 #include <com/sun/star/util/XChangesBatch.hpp>
36 #include <com/sun/star/util/XChangesListener.hpp>
37 #include <com/sun/star/util/XChangesNotifier.hpp>
38 #include <comphelper/sequence.hxx>
39 #include <cppu/unotype.hxx>
40 #include <cppuhelper/queryinterface.hxx>
41 #include <cppuhelper/weak.hxx>
42 #include <osl/mutex.hxx>
43 #include <rtl/ref.hxx>
44 #include <rtl/ustring.hxx>
46 #include "broadcaster.hxx"
47 #include "components.hxx"
48 #include "data.hxx"
49 #include "lock.hxx"
50 #include "modifications.hxx"
51 #include "node.hxx"
52 #include "rootaccess.hxx"
54 namespace configmgr {
56 RootAccess::RootAccess(
57 Components & components, OUString const & pathRepresentation,
58 OUString const & locale, bool update):
59 Access(components), pathRepresentation_(pathRepresentation),
60 locale_(locale),
61 lock_( lock() ),
62 update_(update), finalized_(false), alive_(true)
66 std::vector<OUString> RootAccess::getAbsolutePath() {
67 getNode();
68 return path_;
71 void RootAccess::initBroadcaster(
72 Modifications::Node const & modifications, Broadcaster * broadcaster)
74 assert(broadcaster != nullptr);
75 std::vector< css::util::ElementChange > changes;
76 initBroadcasterAndChanges(
77 modifications, broadcaster, changesListeners_.empty() ? nullptr : &changes);
78 if (!changes.empty()) {
79 css::util::ChangesSet set(comphelper::containerToSequence(changes));
80 for (auto const& changesListener : changesListeners_)
82 cppu::OWeakObject* pSource = this;
83 css::uno::Reference< css::uno::XInterface > xBase( pSource, css::uno::UNO_QUERY );
84 broadcaster->addChangesNotification(
85 changesListener,
86 css::util::ChangesEvent(
87 pSource, css::uno::Any( xBase ), set));
92 void RootAccess::acquire() throw () {
93 Access::acquire();
96 void RootAccess::release() throw () {
97 Access::release();
100 OUString const & RootAccess::getAbsolutePathRepresentation() {
101 getNode(); // turn pathRepresentation_ into canonic form
102 return pathRepresentation_;
106 void RootAccess::setAlive(bool b) {
107 alive_ = b;
110 void RootAccess::addChangesListener(
111 css::uno::Reference< css::util::XChangesListener > const & aListener)
113 assert(thisIs(IS_ANY));
115 osl::MutexGuard g(*lock_);
116 checkLocalizedPropertyAccess();
117 if (!aListener.is()) {
118 throw css::uno::RuntimeException(
119 "null listener", static_cast< cppu::OWeakObject * >(this));
121 if (!isDisposed()) {
122 changesListeners_.insert(aListener);
123 return;
126 try {
127 aListener->disposing(
128 css::lang::EventObject(static_cast< cppu::OWeakObject * >(this)));
129 } catch (css::lang::DisposedException &) {}
132 void RootAccess::removeChangesListener(
133 css::uno::Reference< css::util::XChangesListener > const & aListener)
135 assert(thisIs(IS_ANY));
136 osl::MutexGuard g(*lock_);
137 checkLocalizedPropertyAccess();
138 ChangesListeners::iterator i(changesListeners_.find(aListener));
139 if (i != changesListeners_.end()) {
140 changesListeners_.erase(i);
144 void RootAccess::commitChanges()
146 assert(thisIs(IS_UPDATE));
147 if (!alive_)
149 return;
151 Broadcaster bc;
153 osl::MutexGuard g(*lock_);
155 checkLocalizedPropertyAccess();
156 int finalizedLayer;
157 Modifications globalMods;
158 commitChildChanges(
159 ((getComponents().resolvePathRepresentation(
160 pathRepresentation_, nullptr, nullptr, &finalizedLayer)
161 == node_) &&
162 finalizedLayer == Data::NO_LAYER),
163 &globalMods);
164 getComponents().writeModifications();
165 getComponents().initGlobalBroadcaster(globalMods, this, &bc);
167 bc.send();
170 sal_Bool RootAccess::hasPendingChanges() {
171 assert(thisIs(IS_UPDATE));
172 osl::MutexGuard g(*lock_);
173 checkLocalizedPropertyAccess();
174 //TODO: Optimize:
175 std::vector< css::util::ElementChange > changes;
176 reportChildChanges(&changes);
177 return !changes.empty();
180 css::uno::Sequence< ::css::util::ElementChange > RootAccess::getPendingChanges()
182 assert(thisIs(IS_UPDATE));
183 osl::MutexGuard g(*lock_);
184 checkLocalizedPropertyAccess();
185 std::vector< css::util::ElementChange > changes;
186 reportChildChanges(&changes);
187 return comphelper::containerToSequence(changes);
190 RootAccess::~RootAccess()
192 osl::MutexGuard g(*lock_);
193 if (alive_)
194 getComponents().removeRootAccess(this);
197 std::vector<OUString> RootAccess::getRelativePath() {
198 return std::vector<OUString>();
201 OUString RootAccess::getRelativePathRepresentation() {
202 return OUString();
205 rtl::Reference< Node > RootAccess::getNode() {
206 if (!node_.is()) {
207 OUString canonic;
208 int finalizedLayer;
209 node_ = getComponents().resolvePathRepresentation(
210 pathRepresentation_, &canonic, &path_, &finalizedLayer);
211 if (!node_.is()) {
212 throw css::uno::RuntimeException(
213 "cannot find " + pathRepresentation_, nullptr);
214 // RootAccess::queryInterface indirectly calls
215 // RootAccess::getNode, so if this RootAccess were passed out in
216 // RuntimeException.Context, client code that called
217 // queryInterface on it would cause trouble; therefore,
218 // RuntimeException.Context is left null here
220 pathRepresentation_ = canonic;
221 assert(!path_.empty() || node_->kind() == Node::KIND_ROOT);
222 if (!path_.empty()) {
223 name_ = path_.back();
225 finalized_ = finalizedLayer != Data::NO_LAYER;
227 return node_;
230 bool RootAccess::isFinalized() {
231 getNode();
232 return finalized_;
235 OUString RootAccess::getNameInternal() {
236 getNode();
237 return name_;
240 rtl::Reference< RootAccess > RootAccess::getRootAccess() {
241 return this;
244 rtl::Reference< Access > RootAccess::getParentAccess() {
245 return rtl::Reference< Access >();
248 void RootAccess::addTypes(std::vector< css::uno::Type > * types) const {
249 assert(types != nullptr);
250 types->push_back(cppu::UnoType< css::util::XChangesNotifier >::get());
251 types->push_back(cppu::UnoType< css::util::XChangesBatch >::get());
254 void RootAccess::addSupportedServiceNames(
255 std::vector<OUString> * services)
257 assert(services != nullptr);
258 services->push_back("com.sun.star.configuration.AccessRootElement");
259 if (update_) {
260 services->push_back("com.sun.star.configuration.UpdateRootElement");
264 void RootAccess::initDisposeBroadcaster(Broadcaster * broadcaster) {
265 assert(broadcaster != nullptr);
266 for (auto const& changesListener : changesListeners_)
268 broadcaster->addDisposeNotification(
269 changesListener.get(),
270 css::lang::EventObject(static_cast< cppu::OWeakObject * >(this)));
272 Access::initDisposeBroadcaster(broadcaster);
275 void RootAccess::clearListeners() throw() {
276 changesListeners_.clear();
277 Access::clearListeners();
280 css::uno::Any RootAccess::queryInterface(css::uno::Type const & aType)
282 assert(thisIs(IS_ANY));
283 osl::MutexGuard g(*lock_);
284 checkLocalizedPropertyAccess();
285 css::uno::Any res(Access::queryInterface(aType));
286 if (res.hasValue()) {
287 return res;
289 res = cppu::queryInterface(
290 aType, static_cast< css::util::XChangesNotifier * >(this));
291 if (res.hasValue()) {
292 return res;
294 if (!res.hasValue() && update_) {
295 res = cppu::queryInterface(
296 aType, static_cast< css::util::XChangesBatch * >(this));
298 return res;
301 OUString RootAccess::getImplementationName()
303 assert(thisIs(IS_ANY));
304 osl::MutexGuard g(*lock_);
305 checkLocalizedPropertyAccess();
306 return "configmgr.RootAccess";
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */