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 <osl/diagnose.h>
29 using namespace ::com::sun::star
;
30 using namespace ::com::sun::star::uno
;
32 namespace sdext
{ namespace presenter
{
34 const OUString
PresenterConfigurationAccess::msPresenterScreenRootName
=
35 "/org.openoffice.Office.PresenterScreen/";
37 PresenterConfigurationAccess::PresenterConfigurationAccess (
38 const Reference
<XComponentContext
>& rxContext
,
39 const OUString
& rsRootName
,
48 Sequence
<Any
> aCreationArguments(3);
49 aCreationArguments
[0] = makeAny(beans::PropertyValue(
53 beans::PropertyState_DIRECT_VALUE
));
54 aCreationArguments
[1] = makeAny(beans::PropertyValue(
57 makeAny((sal_Int32
)-1),
58 beans::PropertyState_DIRECT_VALUE
));
59 aCreationArguments
[2] = makeAny(beans::PropertyValue(
63 beans::PropertyState_DIRECT_VALUE
));
65 OUString sAccessService
;
66 if (eMode
== READ_ONLY
)
67 sAccessService
= "com.sun.star.configuration.ConfigurationAccess";
69 sAccessService
= "com.sun.star.configuration.ConfigurationUpdateAccess";
71 Reference
<lang::XMultiServiceFactory
> xProvider
=
72 configuration::theDefaultProvider::get( rxContext
);
73 mxRoot
= xProvider
->createInstanceWithArguments(
74 sAccessService
, aCreationArguments
);
78 catch (const Exception
& rException
)
80 OSL_TRACE ("caught exception while opening configuration: %s",
81 OUStringToOString(rException
.Message
,
82 RTL_TEXTENCODING_UTF8
).getStr());
86 PresenterConfigurationAccess::~PresenterConfigurationAccess()
90 bool PresenterConfigurationAccess::IsValid() const
95 Any
PresenterConfigurationAccess::GetConfigurationNode (const OUString
& sPathToNode
)
97 return GetConfigurationNode(
98 Reference
<container::XHierarchicalNameAccess
>(mxRoot
, UNO_QUERY
),
102 bool PresenterConfigurationAccess::GoToChild (const OUString
& rsPathToNode
)
107 Reference
<container::XHierarchicalNameAccess
> xNode (maNode
, UNO_QUERY
);
110 maNode
= GetConfigurationNode(
111 Reference
<container::XHierarchicalNameAccess
>(maNode
, UNO_QUERY
),
113 if (Reference
<XInterface
>(maNode
, UNO_QUERY
).is())
121 bool PresenterConfigurationAccess::GoToChild (const Predicate
& rPredicate
)
126 maNode
= Find(Reference
<container::XNameAccess
>(maNode
,UNO_QUERY
), rPredicate
);
127 if (Reference
<XInterface
>(maNode
, UNO_QUERY
).is())
134 bool PresenterConfigurationAccess::SetProperty (
135 const OUString
& rsPropertyName
,
138 Reference
<beans::XPropertySet
> xProperties (maNode
, UNO_QUERY
);
139 if (xProperties
.is())
141 xProperties
->setPropertyValue(rsPropertyName
, rValue
);
148 Any
PresenterConfigurationAccess::GetConfigurationNode (
149 const css::uno::Reference
<css::container::XHierarchicalNameAccess
>& rxNode
,
150 const OUString
& sPathToNode
)
152 if (sPathToNode
.isEmpty())
159 return rxNode
->getByHierarchicalName(sPathToNode
);
162 catch (const Exception
& rException
)
164 OSL_TRACE ("caught exception while getting configuration node %s: %s",
165 OUStringToOString(sPathToNode
, RTL_TEXTENCODING_UTF8
).getStr(),
166 OUStringToOString(rException
.Message
, RTL_TEXTENCODING_UTF8
).getStr());
172 Reference
<beans::XPropertySet
> PresenterConfigurationAccess::GetNodeProperties (
173 const css::uno::Reference
<css::container::XHierarchicalNameAccess
>& rxNode
,
174 const OUString
& rsPathToNode
)
176 return Reference
<beans::XPropertySet
>(GetConfigurationNode(rxNode
, rsPathToNode
), UNO_QUERY
);
179 void PresenterConfigurationAccess::CommitChanges()
181 Reference
<util::XChangesBatch
> xConfiguration (mxRoot
, UNO_QUERY
);
182 if (xConfiguration
.is())
183 xConfiguration
->commitChanges();
186 void PresenterConfigurationAccess::ForAll (
187 const Reference
<container::XNameAccess
>& rxContainer
,
188 const ::std::vector
<OUString
>& rArguments
,
189 const ItemProcessor
& rProcessor
)
191 if (rxContainer
.is())
193 ::std::vector
<Any
> aValues(rArguments
.size());
194 Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
195 for (sal_Int32 nItemIndex
=0; nItemIndex
<aKeys
.getLength(); ++nItemIndex
)
197 bool bHasAllValues (true);
198 const OUString
& rsKey (aKeys
[nItemIndex
]);
199 Reference
<container::XNameAccess
> xSetItem (rxContainer
->getByName(rsKey
), UNO_QUERY
);
200 Reference
<beans::XPropertySet
> xSet (xSetItem
, UNO_QUERY
);
201 OSL_ASSERT(xSet
.is());
204 // Get from the current item of the container the children
205 // that match the names in the rArguments list.
206 for (sal_uInt32 nValueIndex
=0; nValueIndex
<aValues
.size(); ++nValueIndex
)
208 if ( ! xSetItem
->hasByName(rArguments
[nValueIndex
]))
209 bHasAllValues
= false;
211 aValues
[nValueIndex
] = xSetItem
->getByName(rArguments
[nValueIndex
]);
215 bHasAllValues
= false;
217 rProcessor(rsKey
,aValues
);
222 void PresenterConfigurationAccess::ForAll (
223 const Reference
<container::XNameAccess
>& rxContainer
,
224 const PropertySetProcessor
& rProcessor
)
226 if (rxContainer
.is())
228 Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
229 for (sal_Int32 nItemIndex
=0; nItemIndex
<aKeys
.getLength(); ++nItemIndex
)
231 const OUString
& rsKey (aKeys
[nItemIndex
]);
232 Reference
<beans::XPropertySet
> xSet (rxContainer
->getByName(rsKey
), UNO_QUERY
);
234 rProcessor(rsKey
, xSet
);
239 Any
PresenterConfigurationAccess::Find (
240 const Reference
<container::XNameAccess
>& rxContainer
,
241 const Predicate
& rPredicate
)
243 if (rxContainer
.is())
245 Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
246 for (sal_Int32 nItemIndex
=0; nItemIndex
<aKeys
.getLength(); ++nItemIndex
)
248 Reference
<beans::XPropertySet
> xProperties (
249 rxContainer
->getByName(aKeys
[nItemIndex
]),
251 if (xProperties
.is())
252 if (rPredicate(aKeys
[nItemIndex
], xProperties
))
253 return Any(xProperties
);
259 bool PresenterConfigurationAccess::IsStringPropertyEqual (
260 const OUString
& rsValue
,
261 const OUString
& rsPropertyName
,
262 const css::uno::Reference
<css::beans::XPropertySet
>& rxNode
)
265 if (GetProperty(rxNode
, rsPropertyName
) >>= sValue
)
266 return sValue
== rsValue
;
271 Any
PresenterConfigurationAccess::GetProperty (
272 const Reference
<beans::XPropertySet
>& rxProperties
,
273 const OUString
& rsKey
)
275 OSL_ASSERT(rxProperties
.is());
276 if ( ! rxProperties
.is())
280 Reference
<beans::XPropertySetInfo
> xInfo (rxProperties
->getPropertySetInfo());
282 if ( ! xInfo
->hasPropertyByName(rsKey
))
284 return rxProperties
->getPropertyValue(rsKey
);
286 catch (beans::UnknownPropertyException
&)
292 } } // end of namespace sdext::tools
294 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */