1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "PresenterConfigurationAccess.hxx"
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>
26 #include <comphelper/propertysequence.hxx>
27 #include <osl/diagnose.h>
28 #include <comphelper/diagnose_ex.hxx>
29 #include <sal/log.hxx>
31 using namespace ::com::sun::star
;
32 using namespace ::com::sun::star::uno
;
34 namespace sdext::presenter
{
36 PresenterConfigurationAccess::PresenterConfigurationAccess (
37 const Reference
<XComponentContext
>& rxContext
,
38 const OUString
& rsRootName
,
45 uno::Sequence
<uno::Any
> aCreationArguments(comphelper::InitAnyPropertySequence(
47 {"nodepath", uno::Any(rsRootName
)},
48 {"depth", uno::Any(sal_Int32(-1))}
51 OUString sAccessService
;
52 if (eMode
== READ_ONLY
)
53 sAccessService
= "com.sun.star.configuration.ConfigurationAccess";
55 sAccessService
= "com.sun.star.configuration.ConfigurationUpdateAccess";
57 Reference
<lang::XMultiServiceFactory
> xProvider
=
58 configuration::theDefaultProvider::get( rxContext
);
59 mxRoot
= xProvider
->createInstanceWithArguments(
60 sAccessService
, aCreationArguments
);
64 catch (const Exception
&)
66 DBG_UNHANDLED_EXCEPTION("sdext.presenter", "caught exception while opening configuration");
70 PresenterConfigurationAccess::~PresenterConfigurationAccess()
74 bool PresenterConfigurationAccess::IsValid() const
79 Any
PresenterConfigurationAccess::GetConfigurationNode (const OUString
& sPathToNode
)
81 return GetConfigurationNode(
82 Reference
<container::XHierarchicalNameAccess
>(mxRoot
, UNO_QUERY
),
86 bool PresenterConfigurationAccess::GoToChild (const OUString
& rsPathToNode
)
91 Reference
<container::XHierarchicalNameAccess
> xNode (maNode
, UNO_QUERY
);
94 maNode
= GetConfigurationNode(
95 Reference
<container::XHierarchicalNameAccess
>(maNode
, UNO_QUERY
),
97 if (Reference
<XInterface
>(maNode
, UNO_QUERY
).is())
105 bool PresenterConfigurationAccess::GoToChild (const Predicate
& rPredicate
)
110 maNode
= Find(Reference
<container::XNameAccess
>(maNode
,UNO_QUERY
), rPredicate
);
111 if (Reference
<XInterface
>(maNode
, UNO_QUERY
).is())
118 bool PresenterConfigurationAccess::SetProperty (
119 const OUString
& rsPropertyName
,
122 Reference
<beans::XPropertySet
> xProperties (maNode
, UNO_QUERY
);
123 if (xProperties
.is())
125 xProperties
->setPropertyValue(rsPropertyName
, rValue
);
132 Any
PresenterConfigurationAccess::GetConfigurationNode (
133 const css::uno::Reference
<css::container::XHierarchicalNameAccess
>& rxNode
,
134 const OUString
& sPathToNode
)
136 if (sPathToNode
.isEmpty())
143 return rxNode
->getByHierarchicalName(sPathToNode
);
146 catch (const Exception
&)
148 TOOLS_WARN_EXCEPTION("sdext.presenter", "caught exception while getting configuration node " << sPathToNode
);
154 Reference
<beans::XPropertySet
> PresenterConfigurationAccess::GetNodeProperties (
155 const css::uno::Reference
<css::container::XHierarchicalNameAccess
>& rxNode
,
156 const OUString
& rsPathToNode
)
158 return Reference
<beans::XPropertySet
>(GetConfigurationNode(rxNode
, rsPathToNode
), UNO_QUERY
);
161 void PresenterConfigurationAccess::CommitChanges()
163 Reference
<util::XChangesBatch
> xConfiguration (mxRoot
, UNO_QUERY
);
164 if (xConfiguration
.is())
165 xConfiguration
->commitChanges();
168 void PresenterConfigurationAccess::ForAll (
169 const Reference
<container::XNameAccess
>& rxContainer
,
170 const ::std::vector
<OUString
>& rArguments
,
171 const ItemProcessor
& rProcessor
)
173 if (!rxContainer
.is())
176 ::std::vector
<Any
> aValues(rArguments
.size());
177 const Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
178 for (const OUString
& rsKey
: aKeys
)
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());
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
)
190 if ( ! xSetItem
->hasByName(rArguments
[nValueIndex
]))
191 bHasAllValues
= false;
193 aValues
[nValueIndex
] = xSetItem
->getByName(rArguments
[nValueIndex
]);
197 bHasAllValues
= false;
203 void PresenterConfigurationAccess::ForAll (
204 const Reference
<container::XNameAccess
>& rxContainer
,
205 const PropertySetProcessor
& rProcessor
)
207 if (rxContainer
.is())
209 const Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
210 for (const OUString
& rsKey
: aKeys
)
212 Reference
<beans::XPropertySet
> xSet (rxContainer
->getByName(rsKey
), UNO_QUERY
);
214 rProcessor(rsKey
, xSet
);
219 Any
PresenterConfigurationAccess::Find (
220 const Reference
<container::XNameAccess
>& rxContainer
,
221 const Predicate
& rPredicate
)
223 if (rxContainer
.is())
225 const Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
226 for (const auto& rKey
: aKeys
)
228 Reference
<beans::XPropertySet
> xProperties (
229 rxContainer
->getByName(rKey
),
231 if (xProperties
.is())
232 if (rPredicate(rKey
, xProperties
))
233 return Any(xProperties
);
239 bool PresenterConfigurationAccess::IsStringPropertyEqual (
240 std::u16string_view rsValue
,
241 const OUString
& rsPropertyName
,
242 const css::uno::Reference
<css::beans::XPropertySet
>& rxNode
)
245 if (GetProperty(rxNode
, rsPropertyName
) >>= sValue
)
246 return sValue
== rsValue
;
251 Any
PresenterConfigurationAccess::GetProperty (
252 const Reference
<beans::XPropertySet
>& rxProperties
,
253 const OUString
& rsKey
)
255 OSL_ASSERT(rxProperties
.is());
256 if ( ! rxProperties
.is())
260 Reference
<beans::XPropertySetInfo
> xInfo (rxProperties
->getPropertySetInfo());
262 if ( ! xInfo
->hasPropertyByName(rsKey
))
264 return rxProperties
->getPropertyValue(rsKey
);
266 catch (beans::UnknownPropertyException
&)
272 } // end of namespace sdext::presenter
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */