bump product version to 6.3.0.0.beta1
[LibreOffice.git] / configmgr / source / rootaccess.cxx
blob25673975a9e696faef5b9e9dad6184daee49198d
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 "rootaccess.hxx"
57 namespace configmgr {
59 RootAccess::RootAccess(
60 Components & components, OUString const & pathRepresentation,
61 OUString const & locale, bool update):
62 Access(components), pathRepresentation_(pathRepresentation),
63 locale_(locale),
64 lock_( lock() ),
65 update_(update), finalized_(false), alive_(true)
69 std::vector<OUString> RootAccess::getAbsolutePath() {
70 getNode();
71 return path_;
74 void RootAccess::initBroadcaster(
75 Modifications::Node const & modifications, Broadcaster * broadcaster)
77 assert(broadcaster != nullptr);
78 std::vector< css::util::ElementChange > changes;
79 initBroadcasterAndChanges(
80 modifications, broadcaster, changesListeners_.empty() ? nullptr : &changes);
81 if (!changes.empty()) {
82 css::util::ChangesSet set(comphelper::containerToSequence(changes));
83 for (auto const& changesListener : changesListeners_)
85 cppu::OWeakObject* pSource = static_cast< cppu::OWeakObject * >(this);
86 css::uno::Reference< css::uno::XInterface > xBase( pSource, css::uno::UNO_QUERY );
87 broadcaster->addChangesNotification(
88 changesListener,
89 css::util::ChangesEvent(
90 pSource, css::uno::Any( xBase ), set));
95 void RootAccess::acquire() throw () {
96 Access::acquire();
99 void RootAccess::release() throw () {
100 Access::release();
103 OUString const & RootAccess::getAbsolutePathRepresentation() {
104 getNode(); // turn pathRepresentation_ into canonic form
105 return pathRepresentation_;
109 void RootAccess::setAlive(bool b) {
110 alive_ = b;
113 void RootAccess::addChangesListener(
114 css::uno::Reference< css::util::XChangesListener > const & aListener)
116 assert(thisIs(IS_ANY));
118 osl::MutexGuard g(*lock_);
119 checkLocalizedPropertyAccess();
120 if (!aListener.is()) {
121 throw css::uno::RuntimeException(
122 "null listener", static_cast< cppu::OWeakObject * >(this));
124 if (!isDisposed()) {
125 changesListeners_.insert(aListener);
126 return;
129 try {
130 aListener->disposing(
131 css::lang::EventObject(static_cast< cppu::OWeakObject * >(this)));
132 } catch (css::lang::DisposedException &) {}
135 void RootAccess::removeChangesListener(
136 css::uno::Reference< css::util::XChangesListener > const & aListener)
138 assert(thisIs(IS_ANY));
139 osl::MutexGuard g(*lock_);
140 checkLocalizedPropertyAccess();
141 ChangesListeners::iterator i(changesListeners_.find(aListener));
142 if (i != changesListeners_.end()) {
143 changesListeners_.erase(i);
147 void RootAccess::commitChanges()
149 assert(thisIs(IS_UPDATE));
150 if (!alive_)
152 return;
154 Broadcaster bc;
156 osl::MutexGuard g(*lock_);
158 checkLocalizedPropertyAccess();
159 int finalizedLayer;
160 Modifications globalMods;
161 commitChildChanges(
162 ((getComponents().resolvePathRepresentation(
163 pathRepresentation_, nullptr, nullptr, &finalizedLayer)
164 == node_) &&
165 finalizedLayer == Data::NO_LAYER),
166 &globalMods);
167 getComponents().writeModifications();
168 getComponents().initGlobalBroadcaster(globalMods, this, &bc);
170 bc.send();
173 sal_Bool RootAccess::hasPendingChanges() {
174 assert(thisIs(IS_UPDATE));
175 osl::MutexGuard g(*lock_);
176 checkLocalizedPropertyAccess();
177 //TODO: Optimize:
178 std::vector< css::util::ElementChange > changes;
179 reportChildChanges(&changes);
180 return !changes.empty();
183 css::uno::Sequence< ::css::util::ElementChange > RootAccess::getPendingChanges()
185 assert(thisIs(IS_UPDATE));
186 osl::MutexGuard g(*lock_);
187 checkLocalizedPropertyAccess();
188 std::vector< css::util::ElementChange > changes;
189 reportChildChanges(&changes);
190 return comphelper::containerToSequence(changes);
193 RootAccess::~RootAccess()
195 osl::MutexGuard g(*lock_);
196 if (alive_)
197 getComponents().removeRootAccess(this);
200 std::vector<OUString> RootAccess::getRelativePath() {
201 return std::vector<OUString>();
204 OUString RootAccess::getRelativePathRepresentation() {
205 return OUString();
208 rtl::Reference< Node > RootAccess::getNode() {
209 if (!node_.is()) {
210 OUString canonic;
211 int finalizedLayer;
212 node_ = getComponents().resolvePathRepresentation(
213 pathRepresentation_, &canonic, &path_, &finalizedLayer);
214 if (!node_.is()) {
215 throw css::uno::RuntimeException(
216 "cannot find " + pathRepresentation_, nullptr);
217 // RootAccess::queryInterface indirectly calls
218 // RootAccess::getNode, so if this RootAccess were passed out in
219 // RuntimeException.Context, client code that called
220 // queryInterface on it would cause trouble; therefore,
221 // RuntimeException.Context is left null here
223 pathRepresentation_ = canonic;
224 assert(!path_.empty() || node_->kind() == Node::KIND_ROOT);
225 if (!path_.empty()) {
226 name_ = path_.back();
228 finalized_ = finalizedLayer != Data::NO_LAYER;
230 return node_;
233 bool RootAccess::isFinalized() {
234 getNode();
235 return finalized_;
238 OUString RootAccess::getNameInternal() {
239 getNode();
240 return name_;
243 rtl::Reference< RootAccess > RootAccess::getRootAccess() {
244 return this;
247 rtl::Reference< Access > RootAccess::getParentAccess() {
248 return rtl::Reference< Access >();
251 void RootAccess::addTypes(std::vector< css::uno::Type > * types) const {
252 assert(types != nullptr);
253 types->push_back(cppu::UnoType< css::util::XChangesNotifier >::get());
254 types->push_back(cppu::UnoType< css::util::XChangesBatch >::get());
257 void RootAccess::addSupportedServiceNames(
258 std::vector<OUString> * services)
260 assert(services != nullptr);
261 services->push_back("com.sun.star.configuration.AccessRootElement");
262 if (update_) {
263 services->push_back("com.sun.star.configuration.UpdateRootElement");
267 void RootAccess::initDisposeBroadcaster(Broadcaster * broadcaster) {
268 assert(broadcaster != nullptr);
269 for (auto const& changesListener : changesListeners_)
271 broadcaster->addDisposeNotification(
272 changesListener.get(),
273 css::lang::EventObject(static_cast< cppu::OWeakObject * >(this)));
275 Access::initDisposeBroadcaster(broadcaster);
278 void RootAccess::clearListeners() throw() {
279 changesListeners_.clear();
280 Access::clearListeners();
283 css::uno::Any RootAccess::queryInterface(css::uno::Type const & aType)
285 assert(thisIs(IS_ANY));
286 osl::MutexGuard g(*lock_);
287 checkLocalizedPropertyAccess();
288 css::uno::Any res(Access::queryInterface(aType));
289 if (res.hasValue()) {
290 return res;
292 res = cppu::queryInterface(
293 aType, static_cast< css::util::XChangesNotifier * >(this));
294 if (res.hasValue()) {
295 return res;
297 if (!res.hasValue() && update_) {
298 res = cppu::queryInterface(
299 aType, static_cast< css::util::XChangesBatch * >(this));
301 return res;
304 OUString RootAccess::getImplementationName()
306 assert(thisIs(IS_ANY));
307 osl::MutexGuard g(*lock_);
308 checkLocalizedPropertyAccess();
309 return OUString("configmgr.RootAccess");
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */