1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PresenterConfigurationAccess.cxx,v $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sdext.hxx"
35 #include "PresenterConfigurationAccess.hxx"
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <com/sun/star/beans/PropertyValue.hpp>
39 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
40 #include <com/sun/star/util/XChangesBatch.hpp>
42 using namespace ::com::sun::star
;
43 using namespace ::com::sun::star::uno
;
44 using ::rtl::OUString
;
46 #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
48 namespace sdext
{ namespace presenter
{
50 const ::rtl::OUString
PresenterConfigurationAccess::msPresenterScreenRootName
=
51 A2S("/org.openoffice.Office.extension.PresenterScreen/");
53 PresenterConfigurationAccess::PresenterConfigurationAccess (
54 const Reference
<XComponentContext
>& rxContext
,
55 const OUString
& rsRootName
,
62 Reference
<lang::XMultiComponentFactory
> xFactory (rxContext
->getServiceManager());
65 Sequence
<Any
> aCreationArguments(3);
66 aCreationArguments
[0] = makeAny(beans::PropertyValue(
70 beans::PropertyState_DIRECT_VALUE
));
71 aCreationArguments
[1] = makeAny(beans::PropertyValue(
74 makeAny((sal_Int32
)-1),
75 beans::PropertyState_DIRECT_VALUE
));
76 aCreationArguments
[2] = makeAny(beans::PropertyValue(
80 beans::PropertyState_DIRECT_VALUE
));
82 OUString sAccessService
;
83 if (eMode
== READ_ONLY
)
84 sAccessService
= A2S("com.sun.star.configuration.ConfigurationAccess");
86 sAccessService
= A2S("com.sun.star.configuration.ConfigurationUpdateAccess");
88 Reference
<lang::XMultiServiceFactory
> xProvider (
89 xFactory
->createInstanceWithContext(
90 A2S("com.sun.star.configuration.ConfigurationProvider"),
93 mxRoot
= xProvider
->createInstanceWithArguments(
94 sAccessService
, aCreationArguments
);
98 catch (Exception
& rException
)
100 OSL_TRACE ("caught exception while opening configuration: %s",
101 ::rtl::OUStringToOString(rException
.Message
,
102 RTL_TEXTENCODING_UTF8
).getStr());
109 PresenterConfigurationAccess::~PresenterConfigurationAccess (void)
116 bool PresenterConfigurationAccess::IsValid (void) const
124 Any
PresenterConfigurationAccess::GetConfigurationNode (const OUString
& sPathToNode
)
126 return GetConfigurationNode(
127 Reference
<container::XHierarchicalNameAccess
>(mxRoot
, UNO_QUERY
),
134 Reference
<beans::XPropertySet
> PresenterConfigurationAccess::GetNodeProperties (
135 const OUString
& sPathToNode
)
137 return GetNodeProperties(
138 Reference
<container::XHierarchicalNameAccess
>(mxRoot
, UNO_QUERY
),
145 bool PresenterConfigurationAccess::GoToChild (const ::rtl::OUString
& rsPathToNode
)
150 Reference
<container::XHierarchicalNameAccess
> xNode (maNode
, UNO_QUERY
);
153 maNode
= GetConfigurationNode(
154 Reference
<container::XHierarchicalNameAccess
>(maNode
, UNO_QUERY
),
156 if (Reference
<XInterface
>(maNode
, UNO_QUERY
).is())
167 bool PresenterConfigurationAccess::GoToChild (const Predicate
& rPredicate
)
172 maNode
= Find(Reference
<container::XNameAccess
>(maNode
,UNO_QUERY
), rPredicate
);
173 if (Reference
<XInterface
>(maNode
, UNO_QUERY
).is())
183 bool PresenterConfigurationAccess::SetProperty (
184 const ::rtl::OUString
& rsPropertyName
,
187 Reference
<beans::XPropertySet
> xProperties (maNode
, UNO_QUERY
);
188 if (xProperties
.is())
190 xProperties
->setPropertyValue(rsPropertyName
, rValue
);
200 Any
PresenterConfigurationAccess::GetConfigurationNode (
201 const css::uno::Reference
<css::container::XHierarchicalNameAccess
>& rxNode
,
202 const OUString
& sPathToNode
)
204 if (sPathToNode
.getLength() == 0)
211 return rxNode
->getByHierarchicalName(sPathToNode
);
214 catch (Exception
& rException
)
216 OSL_TRACE ("caught exception while getting configuration node %s: %s",
217 ::rtl::OUStringToOString(sPathToNode
, RTL_TEXTENCODING_UTF8
).getStr(),
218 ::rtl::OUStringToOString(rException
.Message
, RTL_TEXTENCODING_UTF8
).getStr());
227 Reference
<beans::XPropertySet
> PresenterConfigurationAccess::GetNodeProperties (
228 const css::uno::Reference
<css::container::XHierarchicalNameAccess
>& rxNode
,
229 const ::rtl::OUString
& rsPathToNode
)
231 return Reference
<beans::XPropertySet
>(GetConfigurationNode(rxNode
, rsPathToNode
), UNO_QUERY
);
237 void PresenterConfigurationAccess::CommitChanges (void)
239 Reference
<util::XChangesBatch
> xConfiguration (mxRoot
, UNO_QUERY
);
240 if (xConfiguration
.is())
241 xConfiguration
->commitChanges();
247 Any
PresenterConfigurationAccess::GetValue (const rtl::OUString
& sKey
)
249 Reference
<container::XNameAccess
> xAccess (GetConfigurationNode(sKey
), UNO_QUERY
);
252 return xAccess
->getByName(sKey
);
263 void PresenterConfigurationAccess::ForAll (
264 const Reference
<container::XNameAccess
>& rxContainer
,
265 const ::std::vector
<OUString
>& rArguments
,
266 const ItemProcessor
& rProcessor
)
268 if (rxContainer
.is())
270 ::std::vector
<Any
> aValues(rArguments
.size());
271 Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
272 for (sal_Int32 nItemIndex
=0; nItemIndex
<aKeys
.getLength(); ++nItemIndex
)
274 bool bHasAllValues (true);
275 const OUString
& rsKey (aKeys
[nItemIndex
]);
276 Reference
<container::XNameAccess
> xSetItem (rxContainer
->getByName(rsKey
), UNO_QUERY
);
277 Reference
<beans::XPropertySet
> xSet (xSetItem
, UNO_QUERY
);
278 OSL_ASSERT(xSet
.is());
281 // Get from the current item of the container the children
282 // that match the names in the rArguments list.
283 for (sal_uInt32 nValueIndex
=0; nValueIndex
<aValues
.size(); ++nValueIndex
)
285 if ( ! xSetItem
->hasByName(rArguments
[nValueIndex
]))
286 bHasAllValues
= false;
288 aValues
[nValueIndex
] = xSetItem
->getByName(rArguments
[nValueIndex
]);
292 bHasAllValues
= false;
294 rProcessor(rsKey
,aValues
);
302 void PresenterConfigurationAccess::ForAll (
303 const Reference
<container::XNameAccess
>& rxContainer
,
304 const PropertySetProcessor
& rProcessor
)
306 if (rxContainer
.is())
308 Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
309 for (sal_Int32 nItemIndex
=0; nItemIndex
<aKeys
.getLength(); ++nItemIndex
)
311 const OUString
& rsKey (aKeys
[nItemIndex
]);
312 Reference
<beans::XPropertySet
> xSet (rxContainer
->getByName(rsKey
), UNO_QUERY
);
314 rProcessor(rsKey
, xSet
);
322 void PresenterConfigurationAccess::FillList(
323 const Reference
<container::XNameAccess
>& rxContainer
,
324 const ::rtl::OUString
& rsArgument
,
325 ::std::vector
<OUString
>& rList
)
329 if (rxContainer
.is())
331 Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
332 rList
.resize(aKeys
.getLength());
333 for (sal_Int32 nItemIndex
=0; nItemIndex
<aKeys
.getLength(); ++nItemIndex
)
335 Reference
<container::XNameAccess
> xSetItem (
336 rxContainer
->getByName(aKeys
[nItemIndex
]), UNO_QUERY
);
339 xSetItem
->getByName(rsArgument
) >>= rList
[nItemIndex
];
344 catch (RuntimeException
&)
351 Any
PresenterConfigurationAccess::Find (
352 const Reference
<container::XNameAccess
>& rxContainer
,
353 const Predicate
& rPredicate
)
355 if (rxContainer
.is())
357 Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
358 for (sal_Int32 nItemIndex
=0; nItemIndex
<aKeys
.getLength(); ++nItemIndex
)
360 Reference
<beans::XPropertySet
> xProperties (
361 rxContainer
->getByName(aKeys
[nItemIndex
]),
363 if (xProperties
.is())
364 if (rPredicate(aKeys
[nItemIndex
], xProperties
))
365 return Any(xProperties
);
374 bool PresenterConfigurationAccess::IsStringPropertyEqual (
375 const ::rtl::OUString
& rsValue
,
376 const ::rtl::OUString
& rsPropertyName
,
377 const css::uno::Reference
<css::beans::XPropertySet
>& rxNode
)
380 if (GetProperty(rxNode
, rsPropertyName
) >>= sValue
)
381 return sValue
== rsValue
;
389 Any
PresenterConfigurationAccess::GetProperty (
390 const Reference
<beans::XPropertySet
>& rxProperties
,
391 const OUString
& rsKey
)
393 OSL_ASSERT(rxProperties
.is());
394 if ( ! rxProperties
.is())
398 Reference
<beans::XPropertySetInfo
> xInfo (rxProperties
->getPropertySetInfo());
400 if ( ! xInfo
->hasPropertyByName(rsKey
))
402 return rxProperties
->getPropertyValue(rsKey
);
404 catch (beans::UnknownPropertyException
&)
413 } } // end of namespace sdext::tools