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 <tools/diagnose_ex.h>
29 #include <sal/log.hxx>
31 using namespace ::com::sun::star
;
32 using namespace ::com::sun::star::uno
;
34 namespace sdext
{ namespace presenter
{
36 const OUString
PresenterConfigurationAccess::msPresenterScreenRootName
=
37 "/org.openoffice.Office.PresenterScreen/";
39 PresenterConfigurationAccess::PresenterConfigurationAccess (
40 const Reference
<XComponentContext
>& rxContext
,
41 const OUString
& rsRootName
,
50 uno::Sequence
<uno::Any
> aCreationArguments(comphelper::InitAnyPropertySequence(
52 {"nodepath", uno::Any(rsRootName
)},
53 {"depth", uno::Any(sal_Int32(-1))}
56 OUString sAccessService
;
57 if (eMode
== READ_ONLY
)
58 sAccessService
= "com.sun.star.configuration.ConfigurationAccess";
60 sAccessService
= "com.sun.star.configuration.ConfigurationUpdateAccess";
62 Reference
<lang::XMultiServiceFactory
> xProvider
=
63 configuration::theDefaultProvider::get( rxContext
);
64 mxRoot
= xProvider
->createInstanceWithArguments(
65 sAccessService
, aCreationArguments
);
69 catch (const Exception
&)
71 DBG_UNHANDLED_EXCEPTION("sdext.presenter", "caught exception while opening configuration");
75 PresenterConfigurationAccess::~PresenterConfigurationAccess()
79 bool PresenterConfigurationAccess::IsValid() const
84 Any
PresenterConfigurationAccess::GetConfigurationNode (const OUString
& sPathToNode
)
86 return GetConfigurationNode(
87 Reference
<container::XHierarchicalNameAccess
>(mxRoot
, UNO_QUERY
),
91 bool PresenterConfigurationAccess::GoToChild (const OUString
& rsPathToNode
)
96 Reference
<container::XHierarchicalNameAccess
> xNode (maNode
, UNO_QUERY
);
99 maNode
= GetConfigurationNode(
100 Reference
<container::XHierarchicalNameAccess
>(maNode
, UNO_QUERY
),
102 if (Reference
<XInterface
>(maNode
, UNO_QUERY
).is())
110 bool PresenterConfigurationAccess::GoToChild (const Predicate
& rPredicate
)
115 maNode
= Find(Reference
<container::XNameAccess
>(maNode
,UNO_QUERY
), rPredicate
);
116 if (Reference
<XInterface
>(maNode
, UNO_QUERY
).is())
123 bool PresenterConfigurationAccess::SetProperty (
124 const OUString
& rsPropertyName
,
127 Reference
<beans::XPropertySet
> xProperties (maNode
, UNO_QUERY
);
128 if (xProperties
.is())
130 xProperties
->setPropertyValue(rsPropertyName
, rValue
);
137 Any
PresenterConfigurationAccess::GetConfigurationNode (
138 const css::uno::Reference
<css::container::XHierarchicalNameAccess
>& rxNode
,
139 const OUString
& sPathToNode
)
141 if (sPathToNode
.isEmpty())
148 return rxNode
->getByHierarchicalName(sPathToNode
);
151 catch (const Exception
&)
153 TOOLS_WARN_EXCEPTION("sdext.presenter", "caught exception while getting configuration node " << sPathToNode
);
159 Reference
<beans::XPropertySet
> PresenterConfigurationAccess::GetNodeProperties (
160 const css::uno::Reference
<css::container::XHierarchicalNameAccess
>& rxNode
,
161 const OUString
& rsPathToNode
)
163 return Reference
<beans::XPropertySet
>(GetConfigurationNode(rxNode
, rsPathToNode
), UNO_QUERY
);
166 void PresenterConfigurationAccess::CommitChanges()
168 Reference
<util::XChangesBatch
> xConfiguration (mxRoot
, UNO_QUERY
);
169 if (xConfiguration
.is())
170 xConfiguration
->commitChanges();
173 void PresenterConfigurationAccess::ForAll (
174 const Reference
<container::XNameAccess
>& rxContainer
,
175 const ::std::vector
<OUString
>& rArguments
,
176 const ItemProcessor
& rProcessor
)
178 if (!rxContainer
.is())
181 ::std::vector
<Any
> aValues(rArguments
.size());
182 const Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
183 for (const OUString
& rsKey
: aKeys
)
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());
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
)
195 if ( ! xSetItem
->hasByName(rArguments
[nValueIndex
]))
196 bHasAllValues
= false;
198 aValues
[nValueIndex
] = xSetItem
->getByName(rArguments
[nValueIndex
]);
202 bHasAllValues
= false;
208 void PresenterConfigurationAccess::ForAll (
209 const Reference
<container::XNameAccess
>& rxContainer
,
210 const PropertySetProcessor
& rProcessor
)
212 if (rxContainer
.is())
214 const Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
215 for (const OUString
& rsKey
: aKeys
)
217 Reference
<beans::XPropertySet
> xSet (rxContainer
->getByName(rsKey
), UNO_QUERY
);
219 rProcessor(rsKey
, xSet
);
224 Any
PresenterConfigurationAccess::Find (
225 const Reference
<container::XNameAccess
>& rxContainer
,
226 const Predicate
& rPredicate
)
228 if (rxContainer
.is())
230 const Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
231 for (const auto& rKey
: aKeys
)
233 Reference
<beans::XPropertySet
> xProperties (
234 rxContainer
->getByName(rKey
),
236 if (xProperties
.is())
237 if (rPredicate(rKey
, xProperties
))
238 return Any(xProperties
);
244 bool PresenterConfigurationAccess::IsStringPropertyEqual (
245 const OUString
& rsValue
,
246 const OUString
& rsPropertyName
,
247 const css::uno::Reference
<css::beans::XPropertySet
>& rxNode
)
250 if (GetProperty(rxNode
, rsPropertyName
) >>= sValue
)
251 return sValue
== rsValue
;
256 Any
PresenterConfigurationAccess::GetProperty (
257 const Reference
<beans::XPropertySet
>& rxProperties
,
258 const OUString
& rsKey
)
260 OSL_ASSERT(rxProperties
.is());
261 if ( ! rxProperties
.is())
265 Reference
<beans::XPropertySetInfo
> xInfo (rxProperties
->getPropertySetInfo());
267 if ( ! xInfo
->hasPropertyByName(rsKey
))
269 return rxProperties
->getPropertyValue(rsKey
);
271 catch (beans::UnknownPropertyException
&)
277 } } // end of namespace sdext::tools
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */