xref: /aoo41x/main/configmgr/source/rootaccess.cxx (revision 3a7cf181)
1*3a7cf181SAndrew Rist /**************************************************************
2*3a7cf181SAndrew Rist  *
3*3a7cf181SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*3a7cf181SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*3a7cf181SAndrew Rist  * distributed with this work for additional information
6*3a7cf181SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*3a7cf181SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*3a7cf181SAndrew Rist  * "License"); you may not use this file except in compliance
9*3a7cf181SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*3a7cf181SAndrew Rist  *
11*3a7cf181SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*3a7cf181SAndrew Rist  *
13*3a7cf181SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*3a7cf181SAndrew Rist  * software distributed under the License is distributed on an
15*3a7cf181SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*3a7cf181SAndrew Rist  * KIND, either express or implied.  See the License for the
17*3a7cf181SAndrew Rist  * specific language governing permissions and limitations
18*3a7cf181SAndrew Rist  * under the License.
19*3a7cf181SAndrew Rist  *
20*3a7cf181SAndrew Rist  *************************************************************/
21*3a7cf181SAndrew Rist 
22*3a7cf181SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_configmgr.hxx"
25cdf0e10cSrcweir #include "sal/config.h"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <vector>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "com/sun/star/lang/DisposedException.hpp"
30cdf0e10cSrcweir #include "com/sun/star/lang/EventObject.hpp"
31cdf0e10cSrcweir #include "com/sun/star/lang/WrappedTargetException.hpp"
32cdf0e10cSrcweir #include "com/sun/star/uno/Any.hxx"
33cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx"
34cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
35cdf0e10cSrcweir #include "com/sun/star/uno/Type.hxx"
36cdf0e10cSrcweir #include "com/sun/star/uno/XInterface.hpp"
37cdf0e10cSrcweir #include "com/sun/star/util/ChangesEvent.hpp"
38cdf0e10cSrcweir #include "com/sun/star/util/ChangesSet.hpp"
39cdf0e10cSrcweir #include "com/sun/star/util/ElementChange.hpp"
40cdf0e10cSrcweir #include "com/sun/star/util/XChangesBatch.hpp"
41cdf0e10cSrcweir #include "com/sun/star/util/XChangesListener.hpp"
42cdf0e10cSrcweir #include "com/sun/star/util/XChangesNotifier.hpp"
43cdf0e10cSrcweir #include "comphelper/sequenceasvector.hxx"
44cdf0e10cSrcweir #include "cppu/unotype.hxx"
45cdf0e10cSrcweir #include "cppuhelper/queryinterface.hxx"
46cdf0e10cSrcweir #include "cppuhelper/weak.hxx"
47cdf0e10cSrcweir #include "osl/diagnose.h"
48cdf0e10cSrcweir #include "osl/mutex.hxx"
49cdf0e10cSrcweir #include "rtl/ref.hxx"
50cdf0e10cSrcweir #include "rtl/ustring.h"
51cdf0e10cSrcweir #include "rtl/ustring.hxx"
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #include "broadcaster.hxx"
54cdf0e10cSrcweir #include "childaccess.hxx"
55cdf0e10cSrcweir #include "components.hxx"
56cdf0e10cSrcweir #include "data.hxx"
57cdf0e10cSrcweir #include "lock.hxx"
58cdf0e10cSrcweir #include "modifications.hxx"
59cdf0e10cSrcweir #include "node.hxx"
60cdf0e10cSrcweir #include "path.hxx"
61cdf0e10cSrcweir #include "rootaccess.hxx"
62cdf0e10cSrcweir 
63cdf0e10cSrcweir namespace configmgr {
64cdf0e10cSrcweir 
65cdf0e10cSrcweir namespace {
66cdf0e10cSrcweir 
67cdf0e10cSrcweir namespace css = com::sun::star;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
RootAccess(Components & components,rtl::OUString const & pathRepresentation,rtl::OUString const & locale,bool update)71cdf0e10cSrcweir RootAccess::RootAccess(
72cdf0e10cSrcweir     Components & components, rtl::OUString const & pathRepresentation,
73cdf0e10cSrcweir     rtl::OUString const & locale, bool update):
74cdf0e10cSrcweir     Access(components), pathRepresentation_(pathRepresentation),
75cdf0e10cSrcweir     locale_(locale), update_(update)
76cdf0e10cSrcweir {}
77cdf0e10cSrcweir 
getAbsolutePath()78cdf0e10cSrcweir Path RootAccess::getAbsolutePath() {
79cdf0e10cSrcweir     getNode();
80cdf0e10cSrcweir     return path_;
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
initBroadcaster(Modifications::Node const & modifications,Broadcaster * broadcaster)83cdf0e10cSrcweir void RootAccess::initBroadcaster(
84cdf0e10cSrcweir     Modifications::Node const & modifications, Broadcaster * broadcaster)
85cdf0e10cSrcweir {
86cdf0e10cSrcweir     OSL_ASSERT(broadcaster != 0);
87cdf0e10cSrcweir     comphelper::SequenceAsVector< css::util::ElementChange > changes;
88cdf0e10cSrcweir     initBroadcasterAndChanges(
89cdf0e10cSrcweir         modifications, broadcaster, changesListeners_.empty() ? 0 : &changes);
90cdf0e10cSrcweir     if (!changes.empty()) {
91cdf0e10cSrcweir         css::util::ChangesSet set(changes.getAsConstList());
92cdf0e10cSrcweir         for (ChangesListeners::iterator i(changesListeners_.begin());
93cdf0e10cSrcweir              i != changesListeners_.end(); ++i)
94cdf0e10cSrcweir         {
95cdf0e10cSrcweir             cppu::OWeakObject* pSource = static_cast< cppu::OWeakObject * >(this);
96cdf0e10cSrcweir             css::uno::Reference< css::uno::XInterface > xBase( pSource, css::uno::UNO_QUERY );
97cdf0e10cSrcweir             broadcaster->addChangesNotification(
98cdf0e10cSrcweir                 *i,
99cdf0e10cSrcweir                 css::util::ChangesEvent(
100cdf0e10cSrcweir                     pSource, makeAny( xBase ), set));
101cdf0e10cSrcweir         }
102cdf0e10cSrcweir     }
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
acquire()105cdf0e10cSrcweir void RootAccess::acquire() throw () {
106cdf0e10cSrcweir     Access::acquire();
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
release()109cdf0e10cSrcweir void RootAccess::release() throw () {
110cdf0e10cSrcweir     Access::release();
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
getAbsolutePathRepresentation()113cdf0e10cSrcweir rtl::OUString RootAccess::getAbsolutePathRepresentation() {
114cdf0e10cSrcweir     getNode(); // turn pathRepresentation_ into canonic form
115cdf0e10cSrcweir     return pathRepresentation_;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
getLocale() const118cdf0e10cSrcweir rtl::OUString RootAccess::getLocale() const {
119cdf0e10cSrcweir     return locale_;
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
isUpdate() const122cdf0e10cSrcweir bool RootAccess::isUpdate() const {
123cdf0e10cSrcweir     return update_;
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
~RootAccess()126cdf0e10cSrcweir RootAccess::~RootAccess() {
127cdf0e10cSrcweir     osl::MutexGuard g(lock);
128cdf0e10cSrcweir     getComponents().removeRootAccess(this);
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
getRelativePath()131cdf0e10cSrcweir Path RootAccess::getRelativePath() {
132cdf0e10cSrcweir     return Path();
133cdf0e10cSrcweir }
134cdf0e10cSrcweir 
getRelativePathRepresentation()135cdf0e10cSrcweir rtl::OUString RootAccess::getRelativePathRepresentation() {
136cdf0e10cSrcweir     return rtl::OUString();
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
getNode()139cdf0e10cSrcweir rtl::Reference< Node > RootAccess::getNode() {
140cdf0e10cSrcweir     if (!node_.is()) {
141cdf0e10cSrcweir         rtl::OUString canonic;
142cdf0e10cSrcweir         int finalizedLayer;
143cdf0e10cSrcweir         node_ = getComponents().resolvePathRepresentation(
144cdf0e10cSrcweir             pathRepresentation_, &canonic, &path_, &finalizedLayer);
145cdf0e10cSrcweir         if (!node_.is()) {
146cdf0e10cSrcweir             throw css::uno::RuntimeException(
147cdf0e10cSrcweir                 (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("cannot find ")) +
148cdf0e10cSrcweir                  pathRepresentation_),
149cdf0e10cSrcweir                 0);
150cdf0e10cSrcweir                 // RootAccess::queryInterface indirectly calls
151cdf0e10cSrcweir                 // RootAccess::getNode, so if this RootAccess were passed out in
152cdf0e10cSrcweir                 // RuntimeException.Context, client code that called
153cdf0e10cSrcweir                 // queryInterface on it would cause trouble; therefore,
154cdf0e10cSrcweir                 // RuntimeException.Context is left null here
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir         pathRepresentation_ = canonic;
157cdf0e10cSrcweir         OSL_ASSERT(!path_.empty());
158cdf0e10cSrcweir         name_ = path_.back();
159cdf0e10cSrcweir         finalized_ = finalizedLayer != Data::NO_LAYER;
160cdf0e10cSrcweir     }
161cdf0e10cSrcweir     return node_;
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
isFinalized()164cdf0e10cSrcweir bool RootAccess::isFinalized() {
165cdf0e10cSrcweir     getNode();
166cdf0e10cSrcweir     return finalized_;
167cdf0e10cSrcweir }
168cdf0e10cSrcweir 
getNameInternal()169cdf0e10cSrcweir rtl::OUString RootAccess::getNameInternal() {
170cdf0e10cSrcweir     getNode();
171cdf0e10cSrcweir     return name_;
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
getRootAccess()174cdf0e10cSrcweir rtl::Reference< RootAccess > RootAccess::getRootAccess() {
175cdf0e10cSrcweir     return this;
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
getParentAccess()178cdf0e10cSrcweir rtl::Reference< Access > RootAccess::getParentAccess() {
179cdf0e10cSrcweir     return rtl::Reference< Access >();
180cdf0e10cSrcweir }
181cdf0e10cSrcweir 
addTypes(std::vector<css::uno::Type> * types) const182cdf0e10cSrcweir void RootAccess::addTypes(std::vector< css::uno::Type > * types) const {
183cdf0e10cSrcweir     OSL_ASSERT(types != 0);
184cdf0e10cSrcweir     types->push_back(cppu::UnoType< css::util::XChangesNotifier >::get());
185cdf0e10cSrcweir     types->push_back(cppu::UnoType< css::util::XChangesBatch >::get());
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
addSupportedServiceNames(std::vector<rtl::OUString> * services)188cdf0e10cSrcweir void RootAccess::addSupportedServiceNames(
189cdf0e10cSrcweir     std::vector< rtl::OUString > * services)
190cdf0e10cSrcweir {
191cdf0e10cSrcweir     OSL_ASSERT(services != 0);
192cdf0e10cSrcweir     services->push_back(
193cdf0e10cSrcweir         rtl::OUString(
194cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
195cdf0e10cSrcweir                 "com.sun.star.configuration.AccessRootElement")));
196cdf0e10cSrcweir     if (update_) {
197cdf0e10cSrcweir         services->push_back(
198cdf0e10cSrcweir             rtl::OUString(
199cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
200cdf0e10cSrcweir                     "com.sun.star.configuration.UpdateRootElement")));
201cdf0e10cSrcweir     }
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
initDisposeBroadcaster(Broadcaster * broadcaster)204cdf0e10cSrcweir void RootAccess::initDisposeBroadcaster(Broadcaster * broadcaster) {
205cdf0e10cSrcweir     OSL_ASSERT(broadcaster != 0);
206cdf0e10cSrcweir     for (ChangesListeners::iterator i(changesListeners_.begin());
207cdf0e10cSrcweir          i != changesListeners_.end(); ++i)
208cdf0e10cSrcweir     {
209cdf0e10cSrcweir         broadcaster->addDisposeNotification(
210cdf0e10cSrcweir             i->get(),
211cdf0e10cSrcweir             css::lang::EventObject(static_cast< cppu::OWeakObject * >(this)));
212cdf0e10cSrcweir     }
213cdf0e10cSrcweir     Access::initDisposeBroadcaster(broadcaster);
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
clearListeners()216cdf0e10cSrcweir void RootAccess::clearListeners() throw() {
217cdf0e10cSrcweir     changesListeners_.clear();
218cdf0e10cSrcweir     Access::clearListeners();
219cdf0e10cSrcweir }
220cdf0e10cSrcweir 
queryInterface(css::uno::Type const & aType)221cdf0e10cSrcweir css::uno::Any RootAccess::queryInterface(css::uno::Type const & aType)
222cdf0e10cSrcweir     throw (css::uno::RuntimeException)
223cdf0e10cSrcweir {
224cdf0e10cSrcweir     OSL_ASSERT(thisIs(IS_ANY));
225cdf0e10cSrcweir     osl::MutexGuard g(lock);
226cdf0e10cSrcweir     checkLocalizedPropertyAccess();
227cdf0e10cSrcweir     css::uno::Any res(Access::queryInterface(aType));
228cdf0e10cSrcweir     if (res.hasValue()) {
229cdf0e10cSrcweir         return res;
230cdf0e10cSrcweir     }
231cdf0e10cSrcweir     res = cppu::queryInterface(
232cdf0e10cSrcweir         aType, static_cast< css::util::XChangesNotifier * >(this));
233cdf0e10cSrcweir     if (res.hasValue()) {
234cdf0e10cSrcweir         return res;
235cdf0e10cSrcweir     }
236cdf0e10cSrcweir     if (!res.hasValue() && update_) {
237cdf0e10cSrcweir         res = cppu::queryInterface(
238cdf0e10cSrcweir             aType, static_cast< css::util::XChangesBatch * >(this));
239cdf0e10cSrcweir     }
240cdf0e10cSrcweir     return res;
241cdf0e10cSrcweir }
242cdf0e10cSrcweir 
addChangesListener(css::uno::Reference<css::util::XChangesListener> const & aListener)243cdf0e10cSrcweir void RootAccess::addChangesListener(
244cdf0e10cSrcweir     css::uno::Reference< css::util::XChangesListener > const & aListener)
245cdf0e10cSrcweir     throw (css::uno::RuntimeException)
246cdf0e10cSrcweir {
247cdf0e10cSrcweir     OSL_ASSERT(thisIs(IS_ANY));
248cdf0e10cSrcweir     {
249cdf0e10cSrcweir         osl::MutexGuard g(lock);
250cdf0e10cSrcweir         checkLocalizedPropertyAccess();
251cdf0e10cSrcweir         if (!aListener.is()) {
252cdf0e10cSrcweir             throw css::uno::RuntimeException(
253cdf0e10cSrcweir                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("null listener")),
254cdf0e10cSrcweir                 static_cast< cppu::OWeakObject * >(this));
255cdf0e10cSrcweir         }
256cdf0e10cSrcweir         if (!isDisposed()) {
257cdf0e10cSrcweir             changesListeners_.insert(aListener);
258cdf0e10cSrcweir             return;
259cdf0e10cSrcweir         }
260cdf0e10cSrcweir     }
261cdf0e10cSrcweir     try {
262cdf0e10cSrcweir         aListener->disposing(
263cdf0e10cSrcweir             css::lang::EventObject(static_cast< cppu::OWeakObject * >(this)));
264cdf0e10cSrcweir     } catch (css::lang::DisposedException &) {}
265cdf0e10cSrcweir }
266cdf0e10cSrcweir 
removeChangesListener(css::uno::Reference<css::util::XChangesListener> const & aListener)267cdf0e10cSrcweir void RootAccess::removeChangesListener(
268cdf0e10cSrcweir     css::uno::Reference< css::util::XChangesListener > const & aListener)
269cdf0e10cSrcweir     throw (css::uno::RuntimeException)
270cdf0e10cSrcweir {
271cdf0e10cSrcweir     OSL_ASSERT(thisIs(IS_ANY));
272cdf0e10cSrcweir     osl::MutexGuard g(lock);
273cdf0e10cSrcweir     checkLocalizedPropertyAccess();
274cdf0e10cSrcweir     ChangesListeners::iterator i(changesListeners_.find(aListener));
275cdf0e10cSrcweir     if (i != changesListeners_.end()) {
276cdf0e10cSrcweir         changesListeners_.erase(i);
277cdf0e10cSrcweir     }
278cdf0e10cSrcweir }
279cdf0e10cSrcweir 
commitChanges()280cdf0e10cSrcweir void RootAccess::commitChanges()
281cdf0e10cSrcweir     throw (css::lang::WrappedTargetException, css::uno::RuntimeException)
282cdf0e10cSrcweir {
283cdf0e10cSrcweir     OSL_ASSERT(thisIs(IS_UPDATE));
284cdf0e10cSrcweir     Broadcaster bc;
285cdf0e10cSrcweir     {
286cdf0e10cSrcweir         osl::MutexGuard g(lock);
287cdf0e10cSrcweir         checkLocalizedPropertyAccess();
288cdf0e10cSrcweir         int finalizedLayer;
289cdf0e10cSrcweir         Modifications globalMods;
290cdf0e10cSrcweir         commitChildChanges(
291cdf0e10cSrcweir             ((getComponents().resolvePathRepresentation(
292cdf0e10cSrcweir                   pathRepresentation_, 0, 0, &finalizedLayer)
293cdf0e10cSrcweir               == node_) &&
294cdf0e10cSrcweir              finalizedLayer == Data::NO_LAYER),
295cdf0e10cSrcweir             &globalMods);
296cdf0e10cSrcweir         getComponents().writeModifications();
297cdf0e10cSrcweir         getComponents().initGlobalBroadcaster(globalMods, this, &bc);
298cdf0e10cSrcweir     }
299cdf0e10cSrcweir     bc.send();
300cdf0e10cSrcweir }
301cdf0e10cSrcweir 
hasPendingChanges()302cdf0e10cSrcweir sal_Bool RootAccess::hasPendingChanges() throw (css::uno::RuntimeException) {
303cdf0e10cSrcweir     OSL_ASSERT(thisIs(IS_UPDATE));
304cdf0e10cSrcweir     osl::MutexGuard g(lock);
305cdf0e10cSrcweir     checkLocalizedPropertyAccess();
306cdf0e10cSrcweir     //TODO: Optimize:
307cdf0e10cSrcweir     std::vector< css::util::ElementChange > changes;
308cdf0e10cSrcweir     reportChildChanges(&changes);
309cdf0e10cSrcweir     return !changes.empty();
310cdf0e10cSrcweir }
311cdf0e10cSrcweir 
getPendingChanges()312cdf0e10cSrcweir css::util::ChangesSet RootAccess::getPendingChanges()
313cdf0e10cSrcweir     throw (css::uno::RuntimeException)
314cdf0e10cSrcweir {
315cdf0e10cSrcweir     OSL_ASSERT(thisIs(IS_UPDATE));
316cdf0e10cSrcweir     osl::MutexGuard g(lock);
317cdf0e10cSrcweir     checkLocalizedPropertyAccess();
318cdf0e10cSrcweir     comphelper::SequenceAsVector< css::util::ElementChange > changes;
319cdf0e10cSrcweir     reportChildChanges(&changes);
320cdf0e10cSrcweir     return changes.getAsConstList();
321cdf0e10cSrcweir }
322cdf0e10cSrcweir 
323cdf0e10cSrcweir }
324