LibreOffice Module sdext (master)  1
PresenterConfigurationAccess.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 
21 
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
24 #include <com/sun/star/configuration/theDefaultProvider.hpp>
25 #include <com/sun/star/util/XChangesBatch.hpp>
27 #include <osl/diagnose.h>
28 #include <tools/diagnose_ex.h>
29 #include <sal/log.hxx>
30 
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::uno;
33 
34 namespace sdext::presenter {
35 
37  "/org.openoffice.Office.PresenterScreen/";
38 
40  const Reference<XComponentContext>& rxContext,
41  const OUString& rsRootName,
42  WriteMode eMode)
43  : mxRoot(),
44  maNode()
45 {
46  try
47  {
48  if (rxContext.is())
49  {
50  uno::Sequence<uno::Any> aCreationArguments(comphelper::InitAnyPropertySequence(
51  {
52  {"nodepath", uno::Any(rsRootName)},
53  {"depth", uno::Any(sal_Int32(-1))}
54  }));
55 
56  OUString sAccessService;
57  if (eMode == READ_ONLY)
58  sAccessService = "com.sun.star.configuration.ConfigurationAccess";
59  else
60  sAccessService = "com.sun.star.configuration.ConfigurationUpdateAccess";
61 
62  Reference<lang::XMultiServiceFactory> xProvider =
63  configuration::theDefaultProvider::get( rxContext );
64  mxRoot = xProvider->createInstanceWithArguments(
65  sAccessService, aCreationArguments);
66  maNode <<= mxRoot;
67  }
68  }
69  catch (const Exception&)
70  {
71  DBG_UNHANDLED_EXCEPTION("sdext.presenter", "caught exception while opening configuration");
72  }
73 }
74 
76 {
77 }
78 
80 {
81  return mxRoot.is();
82 }
83 
85 {
86  return GetConfigurationNode(
87  Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
88  sPathToNode);
89 }
90 
91 bool PresenterConfigurationAccess::GoToChild (const OUString& rsPathToNode)
92 {
93  if ( ! IsValid())
94  return false;
95 
96  Reference<container::XHierarchicalNameAccess> xNode (maNode, UNO_QUERY);
97  if (xNode.is())
98  {
100  Reference<container::XHierarchicalNameAccess>(maNode, UNO_QUERY),
101  rsPathToNode);
102  if (Reference<XInterface>(maNode, UNO_QUERY).is())
103  return true;
104  }
105 
106  mxRoot = nullptr;
107  return false;
108 }
109 
111 {
112  if ( ! IsValid())
113  return false;
114 
115  maNode = Find(Reference<container::XNameAccess>(maNode,UNO_QUERY), rPredicate);
116  if (Reference<XInterface>(maNode, UNO_QUERY).is())
117  return true;
118 
119  mxRoot = nullptr;
120  return false;
121 }
122 
124  const OUString& rsPropertyName,
125  const Any& rValue)
126 {
127  Reference<beans::XPropertySet> xProperties (maNode, UNO_QUERY);
128  if (xProperties.is())
129  {
130  xProperties->setPropertyValue(rsPropertyName, rValue);
131  return true;
132  }
133  else
134  return false;
135 }
136 
138  const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
139  const OUString& sPathToNode)
140 {
141  if (sPathToNode.isEmpty())
142  return Any(rxNode);
143 
144  try
145  {
146  if (rxNode.is())
147  {
148  return rxNode->getByHierarchicalName(sPathToNode);
149  }
150  }
151  catch (const Exception&)
152  {
153  TOOLS_WARN_EXCEPTION("sdext.presenter", "caught exception while getting configuration node " << sPathToNode);
154  }
155 
156  return Any();
157 }
158 
160  const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
161  const OUString& rsPathToNode)
162 {
163  return Reference<beans::XPropertySet>(GetConfigurationNode(rxNode, rsPathToNode), UNO_QUERY);
164 }
165 
167 {
168  Reference<util::XChangesBatch> xConfiguration (mxRoot, UNO_QUERY);
169  if (xConfiguration.is())
170  xConfiguration->commitChanges();
171 }
172 
174  const Reference<container::XNameAccess>& rxContainer,
175  const ::std::vector<OUString>& rArguments,
176  const ItemProcessor& rProcessor)
177 {
178  if (!rxContainer.is())
179  return;
180 
181  ::std::vector<Any> aValues(rArguments.size());
182  const Sequence<OUString> aKeys (rxContainer->getElementNames());
183  for (const OUString& rsKey : aKeys)
184  {
185  bool bHasAllValues (true);
186  Reference<container::XNameAccess> xSetItem (rxContainer->getByName(rsKey), UNO_QUERY);
187  Reference<beans::XPropertySet> xSet (xSetItem, UNO_QUERY);
188  OSL_ASSERT(xSet.is());
189  if (xSetItem.is())
190  {
191  // Get from the current item of the container the children
192  // that match the names in the rArguments list.
193  for (size_t nValueIndex=0; nValueIndex<aValues.size(); ++nValueIndex)
194  {
195  if ( ! xSetItem->hasByName(rArguments[nValueIndex]))
196  bHasAllValues = false;
197  else
198  aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]);
199  }
200  }
201  else
202  bHasAllValues = false;
203  if (bHasAllValues)
204  rProcessor(aValues);
205  }
206 }
207 
209  const Reference<container::XNameAccess>& rxContainer,
210  const PropertySetProcessor& rProcessor)
211 {
212  if (rxContainer.is())
213  {
214  const Sequence<OUString> aKeys (rxContainer->getElementNames());
215  for (const OUString& rsKey : aKeys)
216  {
217  Reference<beans::XPropertySet> xSet (rxContainer->getByName(rsKey), UNO_QUERY);
218  if (xSet.is())
219  rProcessor(rsKey, xSet);
220  }
221  }
222 }
223 
225  const Reference<container::XNameAccess>& rxContainer,
226  const Predicate& rPredicate)
227 {
228  if (rxContainer.is())
229  {
230  const Sequence<OUString> aKeys (rxContainer->getElementNames());
231  for (const auto& rKey : aKeys)
232  {
233  Reference<beans::XPropertySet> xProperties (
234  rxContainer->getByName(rKey),
235  UNO_QUERY);
236  if (xProperties.is())
237  if (rPredicate(rKey, xProperties))
238  return Any(xProperties);
239  }
240  }
241  return Any();
242 }
243 
245  const OUString& rsValue,
246  const OUString& rsPropertyName,
247  const css::uno::Reference<css::beans::XPropertySet>& rxNode)
248 {
249  OUString sValue;
250  if (GetProperty(rxNode, rsPropertyName) >>= sValue)
251  return sValue == rsValue;
252  else
253  return false;
254 }
255 
257  const Reference<beans::XPropertySet>& rxProperties,
258  const OUString& rsKey)
259 {
260  OSL_ASSERT(rxProperties.is());
261  if ( ! rxProperties.is())
262  return Any();
263  try
264  {
265  Reference<beans::XPropertySetInfo> xInfo (rxProperties->getPropertySetInfo());
266  if (xInfo.is())
267  if ( ! xInfo->hasPropertyByName(rsKey))
268  return Any();
269  return rxProperties->getPropertyValue(rsKey);
270  }
271  catch (beans::UnknownPropertyException&)
272  {
273  }
274  return Any();
275 }
276 
277 } // end of namespace sdext::presenter
278 
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void ForAll(const css::uno::Reference< css::container::XNameAccess > &rxContainer, const ::std::vector< OUString > &rArguments, const ItemProcessor &rProcessor)
Execute a functor for all elements of the given container.
static css::uno::Any GetProperty(const css::uno::Reference< css::beans::XPropertySet > &rxProperties, const OUString &rsKey)
This method wraps a call to getPropertyValue() and returns an empty Any instead of throwing an except...
css::uno::Reference< css::uno::XInterface > mxRoot
void CommitChanges()
Write any changes that have been made back to the configuration.
::std::function< bool(const OUString &, const css::uno::Reference< css::beans::XPropertySet > &)> Predicate
static css::uno::Reference< css::beans::XPropertySet > GetNodeProperties(const css::uno::Reference< css::container::XHierarchicalNameAccess > &rxNode, const OUString &rsPathToNode)
bool SetProperty(const OUString &rsPropertyName, const css::uno::Any &rValue)
Modify the property child of the currently focused node.
bool IsValid() const
Return when opening the configuration (via creating a new PresenterConfigurationAccess object)...
#define DBG_UNHANDLED_EXCEPTION(...)
static css::uno::Any Find(const css::uno::Reference< css::container::XNameAccess > &rxContainer, const Predicate &rPredicate)
#define TOOLS_WARN_EXCEPTION(area, stream)
css::uno::Sequence< css::uno::Any > InitAnyPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
css::uno::Any GetConfigurationNode(const OUString &rsPathToNode)
Return a configuration node below the root of the called object.
PresenterConfigurationAccess(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const OUString &rsRootName, WriteMode eMode)
Create a new object to access the configuration entries below the given root.
bool GoToChild(const OUString &rsPathToNode)
Move the focused node to the (possibly indirect) child specified by the given path.
static bool IsStringPropertyEqual(const OUString &rsValue, const OUString &rsPropertyName, const css::uno::Reference< css::beans::XPropertySet > &rxNode)