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>
28 using namespace ::com::sun::star
;
29 using namespace ::com::sun::star::uno
;
31 namespace sdext
{ namespace presenter
{
33 const OUString
PresenterConfigurationAccess::msPresenterScreenRootName
=
34 "/org.openoffice.Office.PresenterScreen/";
36 PresenterConfigurationAccess::PresenterConfigurationAccess (
37 const Reference
<XComponentContext
>& rxContext
,
38 const OUString
& rsRootName
,
47 Sequence
<Any
> aCreationArguments(3);
48 aCreationArguments
[0] = makeAny(beans::PropertyValue(
52 beans::PropertyState_DIRECT_VALUE
));
53 aCreationArguments
[1] = makeAny(beans::PropertyValue(
56 makeAny((sal_Int32
)-1),
57 beans::PropertyState_DIRECT_VALUE
));
58 aCreationArguments
[2] = makeAny(beans::PropertyValue(
62 beans::PropertyState_DIRECT_VALUE
));
64 OUString sAccessService
;
65 if (eMode
== READ_ONLY
)
66 sAccessService
= "com.sun.star.configuration.ConfigurationAccess";
68 sAccessService
= "com.sun.star.configuration.ConfigurationUpdateAccess";
70 Reference
<lang::XMultiServiceFactory
> xProvider
=
71 configuration::theDefaultProvider::get( rxContext
);
72 mxRoot
= xProvider
->createInstanceWithArguments(
73 sAccessService
, aCreationArguments
);
77 catch (const Exception
& rException
)
79 OSL_TRACE ("caught exception while opening configuration: %s",
80 OUStringToOString(rException
.Message
,
81 RTL_TEXTENCODING_UTF8
).getStr());
85 PresenterConfigurationAccess::~PresenterConfigurationAccess (void)
89 bool PresenterConfigurationAccess::IsValid (void) const
94 Any
PresenterConfigurationAccess::GetConfigurationNode (const OUString
& sPathToNode
)
96 return GetConfigurationNode(
97 Reference
<container::XHierarchicalNameAccess
>(mxRoot
, UNO_QUERY
),
101 bool PresenterConfigurationAccess::GoToChild (const OUString
& rsPathToNode
)
106 Reference
<container::XHierarchicalNameAccess
> xNode (maNode
, UNO_QUERY
);
109 maNode
= GetConfigurationNode(
110 Reference
<container::XHierarchicalNameAccess
>(maNode
, UNO_QUERY
),
112 if (Reference
<XInterface
>(maNode
, UNO_QUERY
).is())
120 bool PresenterConfigurationAccess::GoToChild (const Predicate
& rPredicate
)
125 maNode
= Find(Reference
<container::XNameAccess
>(maNode
,UNO_QUERY
), rPredicate
);
126 if (Reference
<XInterface
>(maNode
, UNO_QUERY
).is())
133 bool PresenterConfigurationAccess::SetProperty (
134 const OUString
& rsPropertyName
,
137 Reference
<beans::XPropertySet
> xProperties (maNode
, UNO_QUERY
);
138 if (xProperties
.is())
140 xProperties
->setPropertyValue(rsPropertyName
, rValue
);
147 Any
PresenterConfigurationAccess::GetConfigurationNode (
148 const css::uno::Reference
<css::container::XHierarchicalNameAccess
>& rxNode
,
149 const OUString
& sPathToNode
)
151 if (sPathToNode
.isEmpty())
158 return rxNode
->getByHierarchicalName(sPathToNode
);
161 catch (const Exception
& rException
)
163 OSL_TRACE ("caught exception while getting configuration node %s: %s",
164 OUStringToOString(sPathToNode
, RTL_TEXTENCODING_UTF8
).getStr(),
165 OUStringToOString(rException
.Message
, RTL_TEXTENCODING_UTF8
).getStr());
171 Reference
<beans::XPropertySet
> PresenterConfigurationAccess::GetNodeProperties (
172 const css::uno::Reference
<css::container::XHierarchicalNameAccess
>& rxNode
,
173 const OUString
& rsPathToNode
)
175 return Reference
<beans::XPropertySet
>(GetConfigurationNode(rxNode
, rsPathToNode
), UNO_QUERY
);
178 void PresenterConfigurationAccess::CommitChanges (void)
180 Reference
<util::XChangesBatch
> xConfiguration (mxRoot
, UNO_QUERY
);
181 if (xConfiguration
.is())
182 xConfiguration
->commitChanges();
185 void PresenterConfigurationAccess::ForAll (
186 const Reference
<container::XNameAccess
>& rxContainer
,
187 const ::std::vector
<OUString
>& rArguments
,
188 const ItemProcessor
& rProcessor
)
190 if (rxContainer
.is())
192 ::std::vector
<Any
> aValues(rArguments
.size());
193 Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
194 for (sal_Int32 nItemIndex
=0; nItemIndex
<aKeys
.getLength(); ++nItemIndex
)
196 bool bHasAllValues (true);
197 const OUString
& rsKey (aKeys
[nItemIndex
]);
198 Reference
<container::XNameAccess
> xSetItem (rxContainer
->getByName(rsKey
), UNO_QUERY
);
199 Reference
<beans::XPropertySet
> xSet (xSetItem
, UNO_QUERY
);
200 OSL_ASSERT(xSet
.is());
203 // Get from the current item of the container the children
204 // that match the names in the rArguments list.
205 for (sal_uInt32 nValueIndex
=0; nValueIndex
<aValues
.size(); ++nValueIndex
)
207 if ( ! xSetItem
->hasByName(rArguments
[nValueIndex
]))
208 bHasAllValues
= false;
210 aValues
[nValueIndex
] = xSetItem
->getByName(rArguments
[nValueIndex
]);
214 bHasAllValues
= false;
216 rProcessor(rsKey
,aValues
);
221 void PresenterConfigurationAccess::ForAll (
222 const Reference
<container::XNameAccess
>& rxContainer
,
223 const PropertySetProcessor
& rProcessor
)
225 if (rxContainer
.is())
227 Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
228 for (sal_Int32 nItemIndex
=0; nItemIndex
<aKeys
.getLength(); ++nItemIndex
)
230 const OUString
& rsKey (aKeys
[nItemIndex
]);
231 Reference
<beans::XPropertySet
> xSet (rxContainer
->getByName(rsKey
), UNO_QUERY
);
233 rProcessor(rsKey
, xSet
);
238 Any
PresenterConfigurationAccess::Find (
239 const Reference
<container::XNameAccess
>& rxContainer
,
240 const Predicate
& rPredicate
)
242 if (rxContainer
.is())
244 Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
245 for (sal_Int32 nItemIndex
=0; nItemIndex
<aKeys
.getLength(); ++nItemIndex
)
247 Reference
<beans::XPropertySet
> xProperties (
248 rxContainer
->getByName(aKeys
[nItemIndex
]),
250 if (xProperties
.is())
251 if (rPredicate(aKeys
[nItemIndex
], xProperties
))
252 return Any(xProperties
);
258 bool PresenterConfigurationAccess::IsStringPropertyEqual (
259 const OUString
& rsValue
,
260 const OUString
& rsPropertyName
,
261 const css::uno::Reference
<css::beans::XPropertySet
>& rxNode
)
264 if (GetProperty(rxNode
, rsPropertyName
) >>= sValue
)
265 return sValue
== rsValue
;
270 Any
PresenterConfigurationAccess::GetProperty (
271 const Reference
<beans::XPropertySet
>& rxProperties
,
272 const OUString
& rsKey
)
274 OSL_ASSERT(rxProperties
.is());
275 if ( ! rxProperties
.is())
279 Reference
<beans::XPropertySetInfo
> xInfo (rxProperties
->getPropertySetInfo());
281 if ( ! xInfo
->hasPropertyByName(rsKey
))
283 return rxProperties
->getPropertyValue(rsKey
);
285 catch (beans::UnknownPropertyException
&)
291 } } // end of namespace sdext::tools
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */