bump product version to 7.2.5.1
[LibreOffice.git] / configmgr / source / rootaccess.cxx
blob17c8f7ec12a28aed992b38b10d5b4a7cc43e3d25
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 return;
81 css::util::ChangesSet set(comphelper::containerToSequence(changes));
82 for (auto const& changesListener : changesListeners_)
84 cppu::OWeakObject* pSource = this;
85 css::uno::Reference< css::uno::XInterface > xBase( pSource, css::uno::UNO_QUERY );
86 broadcaster->addChangesNotification(
87 changesListener,
88 css::util::ChangesEvent(
89 pSource, css::uno::Any( xBase ), set));
93 void RootAccess::acquire() noexcept {
94 Access::acquire();
97 void RootAccess::release() noexcept {
98 Access::release();
101 OUString const & RootAccess::getAbsolutePathRepresentation() {
102 getNode(); // turn pathRepresentation_ into canonic form
103 return pathRepresentation_;
107 void RootAccess::setAlive(bool b) {
108 alive_ = b;
111 void RootAccess::addChangesListener(
112 css::uno::Reference< css::util::XChangesListener > const & aListener)
114 assert(thisIs(IS_ANY));
116 osl::MutexGuard g(*lock_);
117 checkLocalizedPropertyAccess();
118 if (!aListener.is()) {
119 throw css::uno::RuntimeException(
120 "null listener", static_cast< cppu::OWeakObject * >(this));
122 if (!isDisposed()) {
123 changesListeners_.insert(aListener);
124 return;
127 try {
128 aListener->disposing(
129 css::lang::EventObject(static_cast< cppu::OWeakObject * >(this)));
130 } catch (css::lang::DisposedException &) {}
133 void RootAccess::removeChangesListener(
134 css::uno::Reference< css::util::XChangesListener > const & aListener)
136 assert(thisIs(IS_ANY));
137 osl::MutexGuard g(*lock_);
138 checkLocalizedPropertyAccess();
139 ChangesListeners::iterator i(changesListeners_.find(aListener));
140 if (i != changesListeners_.end()) {
141 changesListeners_.erase(i);
145 void RootAccess::commitChanges()
147 assert(thisIs(IS_UPDATE));
148 if (!alive_)
150 return;
152 Broadcaster bc;
154 osl::MutexGuard g(*lock_);
156 checkLocalizedPropertyAccess();
157 int finalizedLayer;
158 Modifications globalMods;
159 commitChildChanges(
160 ((getComponents().resolvePathRepresentation(
161 pathRepresentation_, nullptr, nullptr, &finalizedLayer)
162 == node_) &&
163 finalizedLayer == Data::NO_LAYER),
164 &globalMods);
165 getComponents().writeModifications();
166 getComponents().initGlobalBroadcaster(globalMods, this, &bc);
168 bc.send();
171 sal_Bool RootAccess::hasPendingChanges() {
172 assert(thisIs(IS_UPDATE));
173 osl::MutexGuard g(*lock_);
174 checkLocalizedPropertyAccess();
175 //TODO: Optimize:
176 std::vector< css::util::ElementChange > changes;
177 reportChildChanges(&changes);
178 return !changes.empty();
181 css::uno::Sequence< ::css::util::ElementChange > RootAccess::getPendingChanges()
183 assert(thisIs(IS_UPDATE));
184 osl::MutexGuard g(*lock_);
185 checkLocalizedPropertyAccess();
186 std::vector< css::util::ElementChange > changes;
187 reportChildChanges(&changes);
188 return comphelper::containerToSequence(changes);
191 RootAccess::~RootAccess()
193 osl::MutexGuard g(*lock_);
194 if (alive_)
195 getComponents().removeRootAccess(this);
198 std::vector<OUString> RootAccess::getRelativePath() {
199 return std::vector<OUString>();
202 OUString RootAccess::getRelativePathRepresentation() {
203 return OUString();
206 rtl::Reference< Node > RootAccess::getNode() {
207 if (!node_.is()) {
208 OUString canonic;
209 int finalizedLayer;
210 node_ = getComponents().resolvePathRepresentation(
211 pathRepresentation_, &canonic, &path_, &finalizedLayer);
212 if (!node_.is()) {
213 throw css::uno::RuntimeException(
214 "cannot find " + pathRepresentation_, nullptr);
215 // RootAccess::queryInterface indirectly calls
216 // RootAccess::getNode, so if this RootAccess were passed out in
217 // RuntimeException.Context, client code that called
218 // queryInterface on it would cause trouble; therefore,
219 // RuntimeException.Context is left null here
221 pathRepresentation_ = canonic;
222 assert(!path_.empty() || node_->kind() == Node::KIND_ROOT);
223 if (!path_.empty()) {
224 name_ = path_.back();
226 finalized_ = finalizedLayer != Data::NO_LAYER;
228 return node_;
231 bool RootAccess::isFinalized() {
232 getNode();
233 return finalized_;
236 OUString RootAccess::getNameInternal() {
237 getNode();
238 return name_;
241 rtl::Reference< RootAccess > RootAccess::getRootAccess() {
242 return this;
245 rtl::Reference< Access > RootAccess::getParentAccess() {
246 return rtl::Reference< Access >();
249 void RootAccess::addTypes(std::vector< css::uno::Type > * types) const {
250 assert(types != nullptr);
251 types->push_back(cppu::UnoType< css::util::XChangesNotifier >::get());
252 types->push_back(cppu::UnoType< css::util::XChangesBatch >::get());
255 void RootAccess::addSupportedServiceNames(
256 std::vector<OUString> * services)
258 assert(services != nullptr);
259 services->push_back("com.sun.star.configuration.AccessRootElement");
260 if (update_) {
261 services->push_back("com.sun.star.configuration.UpdateRootElement");
265 void RootAccess::initDisposeBroadcaster(Broadcaster * broadcaster) {
266 assert(broadcaster != nullptr);
267 for (auto const& changesListener : changesListeners_)
269 broadcaster->addDisposeNotification(
270 changesListener,
271 css::lang::EventObject(static_cast< cppu::OWeakObject * >(this)));
273 Access::initDisposeBroadcaster(broadcaster);
276 void RootAccess::clearListeners() noexcept {
277 changesListeners_.clear();
278 Access::clearListeners();
281 css::uno::Any RootAccess::queryInterface(css::uno::Type const & aType)
283 assert(thisIs(IS_ANY));
284 osl::MutexGuard g(*lock_);
285 checkLocalizedPropertyAccess();
286 css::uno::Any res(Access::queryInterface(aType));
287 if (res.hasValue()) {
288 return res;
290 res = cppu::queryInterface(
291 aType, static_cast< css::util::XChangesNotifier * >(this));
292 if (res.hasValue()) {
293 return res;
295 if (!res.hasValue() && update_) {
296 res = cppu::queryInterface(
297 aType, static_cast< css::util::XChangesBatch * >(this));
299 return res;
302 OUString RootAccess::getImplementationName()
304 assert(thisIs(IS_ANY));
305 osl::MutexGuard g(*lock_);
306 checkLocalizedPropertyAccess();
307 return "configmgr.RootAccess";
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */