Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "sal/config.h"
21 :
22 : #include <cassert>
23 : #include <vector>
24 :
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"
47 :
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"
57 :
58 : namespace configmgr {
59 :
60 0 : RootAccess::RootAccess(
61 : Components & components, OUString const & pathRepresentation,
62 : OUString const & locale, bool update):
63 : Access(components), pathRepresentation_(pathRepresentation),
64 0 : locale_(locale), update_(update), finalized_(false), alive_(true)
65 : {
66 0 : lock_ = lock();
67 0 : }
68 :
69 0 : Path RootAccess::getAbsolutePath() {
70 0 : getNode();
71 0 : return path_;
72 : }
73 :
74 0 : void RootAccess::initBroadcaster(
75 : Modifications::Node const & modifications, Broadcaster * broadcaster)
76 : {
77 : assert(broadcaster != 0);
78 0 : comphelper::SequenceAsVector< css::util::ElementChange > changes;
79 : initBroadcasterAndChanges(
80 0 : modifications, broadcaster, changesListeners_.empty() ? 0 : &changes);
81 0 : if (!changes.empty()) {
82 0 : css::util::ChangesSet set(changes.getAsConstList());
83 0 : for (ChangesListeners::iterator i(changesListeners_.begin());
84 0 : i != changesListeners_.end(); ++i)
85 : {
86 0 : cppu::OWeakObject* pSource = static_cast< cppu::OWeakObject * >(this);
87 0 : css::uno::Reference< css::uno::XInterface > xBase( pSource, css::uno::UNO_QUERY );
88 : broadcaster->addChangesNotification(
89 0 : *i,
90 : css::util::ChangesEvent(
91 0 : pSource, makeAny( xBase ), set));
92 0 : }
93 0 : }
94 0 : }
95 :
96 0 : void RootAccess::acquire() throw () {
97 0 : Access::acquire();
98 0 : }
99 :
100 0 : void RootAccess::release() throw () {
101 0 : Access::release();
102 0 : }
103 :
104 0 : OUString RootAccess::getAbsolutePathRepresentation() {
105 0 : getNode(); // turn pathRepresentation_ into canonic form
106 0 : return pathRepresentation_;
107 : }
108 :
109 0 : OUString RootAccess::getLocale() const {
110 0 : return locale_;
111 : }
112 :
113 0 : bool RootAccess::isUpdate() const {
114 0 : return update_;
115 : }
116 :
117 0 : void RootAccess::setAlive(bool b) {
118 0 : alive_ = b;
119 0 : }
120 :
121 0 : void RootAccess::addChangesListener(
122 : css::uno::Reference< css::util::XChangesListener > const & aListener)
123 : throw (css::uno::RuntimeException, std::exception)
124 : {
125 : assert(thisIs(IS_ANY));
126 : {
127 0 : osl::MutexGuard g(*lock_);
128 0 : checkLocalizedPropertyAccess();
129 0 : if (!aListener.is()) {
130 : throw css::uno::RuntimeException(
131 0 : "null listener", static_cast< cppu::OWeakObject * >(this));
132 : }
133 0 : if (!isDisposed()) {
134 0 : changesListeners_.insert(aListener);
135 0 : return;
136 0 : }
137 : }
138 : try {
139 0 : aListener->disposing(
140 0 : css::lang::EventObject(static_cast< cppu::OWeakObject * >(this)));
141 0 : } catch (css::lang::DisposedException &) {}
142 : }
143 :
144 0 : void RootAccess::removeChangesListener(
145 : css::uno::Reference< css::util::XChangesListener > const & aListener)
146 : throw (css::uno::RuntimeException, std::exception)
147 : {
148 : assert(thisIs(IS_ANY));
149 0 : osl::MutexGuard g(*lock_);
150 0 : checkLocalizedPropertyAccess();
151 0 : ChangesListeners::iterator i(changesListeners_.find(aListener));
152 0 : if (i != changesListeners_.end()) {
153 0 : changesListeners_.erase(i);
154 0 : }
155 0 : }
156 :
157 0 : void RootAccess::commitChanges()
158 : throw (css::lang::WrappedTargetException,
159 : css::uno::RuntimeException,
160 : std::exception)
161 : {
162 : assert(thisIs(IS_UPDATE));
163 0 : if (!alive_)
164 : {
165 0 : return;
166 : }
167 0 : Broadcaster bc;
168 : {
169 0 : osl::MutexGuard g(*lock_);
170 :
171 0 : checkLocalizedPropertyAccess();
172 : int finalizedLayer;
173 0 : Modifications globalMods;
174 : commitChildChanges(
175 0 : ((getComponents().resolvePathRepresentation(
176 : pathRepresentation_, 0, 0, &finalizedLayer)
177 0 : == node_) &&
178 0 : finalizedLayer == Data::NO_LAYER),
179 0 : &globalMods);
180 0 : getComponents().writeModifications();
181 0 : getComponents().initGlobalBroadcaster(globalMods, this, &bc);
182 : }
183 0 : bc.send();
184 : }
185 :
186 0 : sal_Bool RootAccess::hasPendingChanges() throw (css::uno::RuntimeException, std::exception) {
187 : assert(thisIs(IS_UPDATE));
188 0 : osl::MutexGuard g(*lock_);
189 0 : checkLocalizedPropertyAccess();
190 : //TODO: Optimize:
191 0 : std::vector< css::util::ElementChange > changes;
192 0 : reportChildChanges(&changes);
193 0 : return !changes.empty();
194 : }
195 :
196 0 : css::util::ChangesSet RootAccess::getPendingChanges()
197 : throw (css::uno::RuntimeException, std::exception)
198 : {
199 : assert(thisIs(IS_UPDATE));
200 0 : osl::MutexGuard g(*lock_);
201 0 : checkLocalizedPropertyAccess();
202 0 : comphelper::SequenceAsVector< css::util::ElementChange > changes;
203 0 : reportChildChanges(&changes);
204 0 : return changes.getAsConstList();
205 : }
206 :
207 0 : RootAccess::~RootAccess()
208 : {
209 0 : osl::MutexGuard g(*lock_);
210 0 : if (alive_)
211 0 : getComponents().removeRootAccess(this);
212 0 : }
213 :
214 0 : Path RootAccess::getRelativePath() {
215 0 : return Path();
216 : }
217 :
218 0 : OUString RootAccess::getRelativePathRepresentation() {
219 0 : return OUString();
220 : }
221 :
222 0 : rtl::Reference< Node > RootAccess::getNode() {
223 0 : if (!node_.is()) {
224 0 : OUString canonic;
225 : int finalizedLayer;
226 0 : node_ = getComponents().resolvePathRepresentation(
227 0 : pathRepresentation_, &canonic, &path_, &finalizedLayer);
228 0 : if (!node_.is()) {
229 : throw css::uno::RuntimeException(
230 0 : "cannot find " + pathRepresentation_, 0);
231 : // RootAccess::queryInterface indirectly calls
232 : // RootAccess::getNode, so if this RootAccess were passed out in
233 : // RuntimeException.Context, client code that called
234 : // queryInterface on it would cause trouble; therefore,
235 : // RuntimeException.Context is left null here
236 : }
237 0 : pathRepresentation_ = canonic;
238 : assert(!path_.empty() || node_->kind() == Node::KIND_ROOT);
239 0 : if (!path_.empty()) {
240 0 : name_ = path_.back();
241 : }
242 0 : finalized_ = finalizedLayer != Data::NO_LAYER;
243 : }
244 0 : return node_;
245 : }
246 :
247 0 : bool RootAccess::isFinalized() {
248 0 : getNode();
249 0 : return finalized_;
250 : }
251 :
252 0 : OUString RootAccess::getNameInternal() {
253 0 : getNode();
254 0 : return name_;
255 : }
256 :
257 0 : rtl::Reference< RootAccess > RootAccess::getRootAccess() {
258 0 : return this;
259 : }
260 :
261 0 : rtl::Reference< Access > RootAccess::getParentAccess() {
262 0 : return rtl::Reference< Access >();
263 : }
264 :
265 0 : void RootAccess::addTypes(std::vector< css::uno::Type > * types) const {
266 : assert(types != 0);
267 0 : types->push_back(cppu::UnoType< css::util::XChangesNotifier >::get());
268 0 : types->push_back(cppu::UnoType< css::util::XChangesBatch >::get());
269 0 : }
270 :
271 0 : void RootAccess::addSupportedServiceNames(
272 : std::vector< OUString > * services)
273 : {
274 : assert(services != 0);
275 0 : services->push_back("com.sun.star.configuration.AccessRootElement");
276 0 : if (update_) {
277 0 : services->push_back("com.sun.star.configuration.UpdateRootElement");
278 : }
279 0 : }
280 :
281 0 : void RootAccess::initDisposeBroadcaster(Broadcaster * broadcaster) {
282 : assert(broadcaster != 0);
283 0 : for (ChangesListeners::iterator i(changesListeners_.begin());
284 0 : i != changesListeners_.end(); ++i)
285 : {
286 : broadcaster->addDisposeNotification(
287 0 : i->get(),
288 0 : css::lang::EventObject(static_cast< cppu::OWeakObject * >(this)));
289 : }
290 0 : Access::initDisposeBroadcaster(broadcaster);
291 0 : }
292 :
293 0 : void RootAccess::clearListeners() throw() {
294 0 : changesListeners_.clear();
295 0 : Access::clearListeners();
296 0 : }
297 :
298 0 : css::uno::Any RootAccess::queryInterface(css::uno::Type const & aType)
299 : throw (css::uno::RuntimeException, std::exception)
300 : {
301 : assert(thisIs(IS_ANY));
302 0 : osl::MutexGuard g(*lock_);
303 0 : checkLocalizedPropertyAccess();
304 0 : css::uno::Any res(Access::queryInterface(aType));
305 0 : if (res.hasValue()) {
306 0 : return res;
307 : }
308 0 : res = cppu::queryInterface(
309 0 : aType, static_cast< css::util::XChangesNotifier * >(this));
310 0 : if (res.hasValue()) {
311 0 : return res;
312 : }
313 0 : if (!res.hasValue() && update_) {
314 0 : res = cppu::queryInterface(
315 0 : aType, static_cast< css::util::XChangesBatch * >(this));
316 : }
317 0 : return res;
318 : }
319 :
320 0 : OUString RootAccess::getImplementationName()
321 : throw (css::uno::RuntimeException, std::exception)
322 : {
323 : assert(thisIs(IS_ANY));
324 0 : osl::MutexGuard g(*lock_);
325 0 : checkLocalizedPropertyAccess();
326 0 : return OUString("configmgr.RootAccess");
327 : }
328 :
329 : }
330 :
331 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|