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/beans/PropertyValue.hpp>
24 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
25 #include <com/sun/star/configuration/theDefaultProvider.hpp>
26 #include <com/sun/star/util/XChangesBatch.hpp>
27 #include <comphelper/propertysequence.hxx>
28 #include <osl/diagnose.h>
29 #include <tools/diagnose_ex.h>
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
& rException
)
153 SAL_WARN("sdext.presenter", "caught exception while getting configuration node " << sPathToNode
<< " : " << rException
);
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())
180 ::std::vector
<Any
> aValues(rArguments
.size());
181 Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
182 for (sal_Int32 nItemIndex
=0; nItemIndex
<aKeys
.getLength(); ++nItemIndex
)
184 bool bHasAllValues (true);
185 const OUString
& rsKey (aKeys
[nItemIndex
]);
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;
209 void PresenterConfigurationAccess::ForAll (
210 const Reference
<container::XNameAccess
>& rxContainer
,
211 const PropertySetProcessor
& rProcessor
)
213 if (rxContainer
.is())
215 Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
216 for (sal_Int32 nItemIndex
=0; nItemIndex
<aKeys
.getLength(); ++nItemIndex
)
218 const OUString
& rsKey (aKeys
[nItemIndex
]);
219 Reference
<beans::XPropertySet
> xSet (rxContainer
->getByName(rsKey
), UNO_QUERY
);
221 rProcessor(rsKey
, xSet
);
226 Any
PresenterConfigurationAccess::Find (
227 const Reference
<container::XNameAccess
>& rxContainer
,
228 const Predicate
& rPredicate
)
230 if (rxContainer
.is())
232 Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
233 for (sal_Int32 nItemIndex
=0; nItemIndex
<aKeys
.getLength(); ++nItemIndex
)
235 Reference
<beans::XPropertySet
> xProperties (
236 rxContainer
->getByName(aKeys
[nItemIndex
]),
238 if (xProperties
.is())
239 if (rPredicate(aKeys
[nItemIndex
], xProperties
))
240 return Any(xProperties
);
246 bool PresenterConfigurationAccess::IsStringPropertyEqual (
247 const OUString
& rsValue
,
248 const OUString
& rsPropertyName
,
249 const css::uno::Reference
<css::beans::XPropertySet
>& rxNode
)
252 if (GetProperty(rxNode
, rsPropertyName
) >>= sValue
)
253 return sValue
== rsValue
;
258 Any
PresenterConfigurationAccess::GetProperty (
259 const Reference
<beans::XPropertySet
>& rxProperties
,
260 const OUString
& rsKey
)
262 OSL_ASSERT(rxProperties
.is());
263 if ( ! rxProperties
.is())
267 Reference
<beans::XPropertySetInfo
> xInfo (rxProperties
->getPropertySetInfo());
269 if ( ! xInfo
->hasPropertyByName(rsKey
))
271 return rxProperties
->getPropertyValue(rsKey
);
273 catch (beans::UnknownPropertyException
&)
279 } } // end of namespace sdext::tools
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */