LibreOffice Module configmgr (master)  1
rootaccess.cxx
Go to the documentation of this file.
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/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>
41 #include <cppuhelper/weak.hxx>
42 #include <osl/mutex.hxx>
43 #include <rtl/ref.hxx>
44 #include <rtl/ustring.hxx>
45 
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"
53 
54 namespace configmgr {
55 
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)
63 {
64 }
65 
66 std::vector<OUString> RootAccess::getAbsolutePath() {
67  getNode();
68  return path_;
69 }
70 
72  Modifications::Node const & modifications, Broadcaster * broadcaster)
73 {
74  assert(broadcaster != nullptr);
75  std::vector< css::util::ElementChange > changes;
77  modifications, broadcaster, changesListeners_.empty() ? nullptr : &changes);
78  if (changes.empty())
79  return;
80 
81  css::util::ChangesSet set(comphelper::containerToSequence(changes));
82  for (auto const& changesListener : changesListeners_)
83  {
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));
90  }
91 }
92 
93 void RootAccess::acquire() throw () {
95 }
96 
97 void RootAccess::release() throw () {
99 }
100 
102  getNode(); // turn pathRepresentation_ into canonic form
103  return pathRepresentation_;
104 }
105 
106 
107 void RootAccess::setAlive(bool b) {
108  alive_ = b;
109 }
110 
112  css::uno::Reference< css::util::XChangesListener > const & aListener)
113 {
114  assert(thisIs(IS_ANY));
115  {
116  osl::MutexGuard g(*lock_);
118  if (!aListener.is()) {
119  throw css::uno::RuntimeException(
120  "null listener", static_cast< cppu::OWeakObject * >(this));
121  }
122  if (!isDisposed()) {
123  changesListeners_.insert(aListener);
124  return;
125  }
126  }
127  try {
128  aListener->disposing(
129  css::lang::EventObject(static_cast< cppu::OWeakObject * >(this)));
130  } catch (css::lang::DisposedException &) {}
131 }
132 
134  css::uno::Reference< css::util::XChangesListener > const & aListener)
135 {
136  assert(thisIs(IS_ANY));
137  osl::MutexGuard g(*lock_);
139  ChangesListeners::iterator i(changesListeners_.find(aListener));
140  if (i != changesListeners_.end()) {
141  changesListeners_.erase(i);
142  }
143 }
144 
146 {
148  if (!alive_)
149  {
150  return;
151  }
152  Broadcaster bc;
153  {
154  osl::MutexGuard g(*lock_);
155 
157  int finalizedLayer;
158  Modifications globalMods;
160  ((getComponents().resolvePathRepresentation(
161  pathRepresentation_, nullptr, nullptr, &finalizedLayer)
162  == node_) &&
163  finalizedLayer == Data::NO_LAYER),
164  &globalMods);
166  getComponents().initGlobalBroadcaster(globalMods, this, &bc);
167  }
168  bc.send();
169 }
170 
173  osl::MutexGuard g(*lock_);
175  //TODO: Optimize:
176  std::vector< css::util::ElementChange > changes;
177  reportChildChanges(&changes);
178  return !changes.empty();
179 }
180 
181 css::uno::Sequence< ::css::util::ElementChange > RootAccess::getPendingChanges()
182 {
184  osl::MutexGuard g(*lock_);
186  std::vector< css::util::ElementChange > changes;
187  reportChildChanges(&changes);
188  return comphelper::containerToSequence(changes);
189 }
190 
192 {
193  osl::MutexGuard g(*lock_);
194  if (alive_)
196 }
197 
198 std::vector<OUString> RootAccess::getRelativePath() {
199  return std::vector<OUString>();
200 }
201 
203  return OUString();
204 }
205 
207  if (!node_.is()) {
208  OUString canonic;
209  int finalizedLayer;
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
220  }
221  pathRepresentation_ = canonic;
222  assert(!path_.empty() || node_->kind() == Node::KIND_ROOT);
223  if (!path_.empty()) {
224  name_ = path_.back();
225  }
226  finalized_ = finalizedLayer != Data::NO_LAYER;
227  }
228  return node_;
229 }
230 
232  getNode();
233  return finalized_;
234 }
235 
237  getNode();
238  return name_;
239 }
240 
242  return this;
243 }
244 
246  return rtl::Reference< Access >();
247 }
248 
249 void RootAccess::addTypes(std::vector< css::uno::Type > * types) const {
250  assert(types != nullptr);
253 }
254 
256  std::vector<OUString> * services)
257 {
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");
262  }
263 }
264 
266  assert(broadcaster != nullptr);
267  for (auto const& changesListener : changesListeners_)
268  {
269  broadcaster->addDisposeNotification(
270  changesListener.get(),
271  css::lang::EventObject(static_cast< cppu::OWeakObject * >(this)));
272  }
273  Access::initDisposeBroadcaster(broadcaster);
274 }
275 
277  changesListeners_.clear();
279 }
280 
281 css::uno::Any RootAccess::queryInterface(css::uno::Type const & aType)
282 {
283  assert(thisIs(IS_ANY));
284  osl::MutexGuard g(*lock_);
286  css::uno::Any res(Access::queryInterface(aType));
287  if (res.hasValue()) {
288  return res;
289  }
290  res = cppu::queryInterface(
291  aType, static_cast< css::util::XChangesNotifier * >(this));
292  if (res.hasValue()) {
293  return res;
294  }
295  if (!res.hasValue() && update_) {
296  res = cppu::queryInterface(
297  aType, static_cast< css::util::XChangesBatch * >(this));
298  }
299  return res;
300 }
301 
303 {
304  assert(thisIs(IS_ANY));
305  osl::MutexGuard g(*lock_);
307  return "configmgr.RootAccess";
308 }
309 
310 }
311 
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString locale_
virtual OUString SAL_CALL getImplementationName() override
Definition: rootaccess.cxx:302
void addChangesNotification(css::uno::Reference< css::util::XChangesListener > const &listener, css::util::ChangesEvent const &event)
Definition: broadcaster.cxx:97
bool isDisposed() const
Definition: access.hxx:344
std::vector< OUString > path_
Definition: rootaccess.hxx:130
rtl::Reference< Node > resolvePathRepresentation(OUString const &pathRepresentation, OUString *canonicRepresentation, std::vector< OUString > *path, int *finalizedLayer) const
Definition: components.cxx:209
Components & getComponents() const
Definition: access.hxx:320
OUString pathRepresentation_
Definition: rootaccess.hxx:128
virtual rtl::Reference< Node > getNode() override
Definition: rootaccess.cxx:206
virtual std::vector< OUString > getRelativePath() override
Definition: rootaccess.cxx:198
virtual ~RootAccess() override
Definition: rootaccess.cxx:191
virtual void SAL_CALL addChangesListener(css::uno::Reference< css::util::XChangesListener > const &aListener) override
Definition: rootaccess.cxx:111
void addDisposeNotification(css::uno::Reference< css::lang::XEventListener > const &listener, css::lang::EventObject const &event)
Definition: broadcaster.cxx:54
void commitChildChanges(bool valid, Modifications *globalModifications)
Definition: access.cxx:1537
virtual css::uno::Sequence< ::css::util::ElementChange > SAL_CALL getPendingChanges() override
Definition: rootaccess.cxx:181
virtual rtl::Reference< Access > getParentAccess() override
Definition: rootaccess.cxx:245
virtual void clearListeners()
Definition: access.cxx:1273
virtual void SAL_CALL acquire() SAL_OVERRIDE
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
virtual std::vector< OUString > getAbsolutePath() override
Definition: rootaccess.cxx:66
virtual OUString getNameInternal() override
Definition: rootaccess.cxx:236
void removeRootAccess(RootAccess *access)
Definition: components.cxx:227
void reportChildChanges(std::vector< css::util::ElementChange > *changes)
Definition: access.cxx:1519
std::shared_ptr< osl::Mutex > lock_
Definition: rootaccess.hxx:135
int i
virtual void initDisposeBroadcaster(Broadcaster *broadcaster) override
Definition: rootaccess.cxx:265
virtual bool isFinalized() override
Definition: rootaccess.cxx:231
rtl::Reference< Node > node_
Definition: rootaccess.hxx:131
void checkLocalizedPropertyAccess()
Definition: access.cxx:1348
unsigned char sal_Bool
virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const &aType) override
Definition: access.cxx:1290
virtual void initDisposeBroadcaster(Broadcaster *broadcaster)
Definition: access.cxx:1222
void setAlive(bool b)
Definition: rootaccess.cxx:107
ChangesListeners changesListeners_
Definition: rootaccess.hxx:133
void initBroadcasterAndChanges(Modifications::Node const &modifications, Broadcaster *broadcaster, std::vector< css::util::ElementChange > *changes)
Definition: access.cxx:1588
bool thisIs(int what)
Definition: access.cxx:2191
virtual OUString getRelativePathRepresentation() override
Definition: rootaccess.cxx:202
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
virtual void initBroadcaster(Modifications::Node const &modifications, Broadcaster *broadcaster) override
Definition: rootaccess.cxx:71
virtual void clearListeners() override
Definition: rootaccess.cxx:276
virtual void addTypes(std::vector< css::uno::Type > *types) const override
Definition: rootaccess.cxx:249
virtual void SAL_CALL release() SAL_OVERRIDE
virtual sal_Bool SAL_CALL hasPendingChanges() override
Definition: rootaccess.cxx:171
virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const &aType) override
Definition: rootaccess.cxx:281
virtual void SAL_CALL release() override
Definition: rootaccess.cxx:97
virtual void SAL_CALL removeChangesListener(css::uno::Reference< css::util::XChangesListener > const &aListener) override
Definition: rootaccess.cxx:133
void initGlobalBroadcaster(Modifications const &modifications, rtl::Reference< RootAccess > const &exclude, Broadcaster *broadcaster)
Definition: components.cxx:231
virtual rtl::Reference< RootAccess > getRootAccess() override
Definition: rootaccess.cxx:241
virtual void SAL_CALL acquire() override
Definition: rootaccess.cxx:93
virtual void SAL_CALL commitChanges() override
Definition: rootaccess.cxx:145
std::shared_ptr< osl::Mutex > lock_
OUString const & getAbsolutePathRepresentation()
Definition: rootaccess.cxx:101
virtual void addSupportedServiceNames(std::vector< OUString > *services) override
Definition: rootaccess.cxx:255
RootAccess(Components &components, OUString const &pathRepresentation, OUString const &locale, bool update)
Definition: rootaccess.cxx:56
std::shared_ptr< osl::Mutex > const & lock()
Definition: lock.cxx:28
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
void set(css::uno::UnoInterfaceReference const &value)