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