Update ooo320-m1
[ooovba.git] / sdext / source / presenter / PresenterConfigurationAccess.cxx
blob303bb56c76dcfb880e4244439c3c9f2d91468a57
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PresenterConfigurationAccess.cxx,v $
11 * $Revision: 1.5 $
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,
56 WriteMode eMode)
57 : mxRoot(),
58 maNode()
60 try
62 Reference<lang::XMultiComponentFactory> xFactory (rxContext->getServiceManager());
63 if (xFactory.is())
65 Sequence<Any> aCreationArguments(3);
66 aCreationArguments[0] = makeAny(beans::PropertyValue(
67 A2S("nodepath"),
68 0,
69 makeAny(rsRootName),
70 beans::PropertyState_DIRECT_VALUE));
71 aCreationArguments[1] = makeAny(beans::PropertyValue(
72 A2S("depth"),
73 0,
74 makeAny((sal_Int32)-1),
75 beans::PropertyState_DIRECT_VALUE));
76 aCreationArguments[2] = makeAny(beans::PropertyValue(
77 A2S("lazywrite"),
78 0,
79 makeAny(true),
80 beans::PropertyState_DIRECT_VALUE));
82 OUString sAccessService;
83 if (eMode == READ_ONLY)
84 sAccessService = A2S("com.sun.star.configuration.ConfigurationAccess");
85 else
86 sAccessService = A2S("com.sun.star.configuration.ConfigurationUpdateAccess");
88 Reference<lang::XMultiServiceFactory> xProvider (
89 xFactory->createInstanceWithContext(
90 A2S("com.sun.star.configuration.ConfigurationProvider"),
91 rxContext),
92 UNO_QUERY_THROW);
93 mxRoot = xProvider->createInstanceWithArguments(
94 sAccessService, aCreationArguments);
95 maNode <<= mxRoot;
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
118 return mxRoot.is();
124 Any PresenterConfigurationAccess::GetConfigurationNode (const OUString& sPathToNode)
126 return GetConfigurationNode(
127 Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
128 sPathToNode);
134 Reference<beans::XPropertySet> PresenterConfigurationAccess::GetNodeProperties (
135 const OUString& sPathToNode)
137 return GetNodeProperties(
138 Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
139 sPathToNode);
145 bool PresenterConfigurationAccess::GoToChild (const ::rtl::OUString& rsPathToNode)
147 if ( ! IsValid())
148 return false;
150 Reference<container::XHierarchicalNameAccess> xNode (maNode, UNO_QUERY);
151 if (xNode.is())
153 maNode = GetConfigurationNode(
154 Reference<container::XHierarchicalNameAccess>(maNode, UNO_QUERY),
155 rsPathToNode);
156 if (Reference<XInterface>(maNode, UNO_QUERY).is())
157 return true;
160 mxRoot = NULL;
161 return false;
167 bool PresenterConfigurationAccess::GoToChild (const Predicate& rPredicate)
169 if ( ! IsValid())
170 return false;
172 maNode = Find(Reference<container::XNameAccess>(maNode,UNO_QUERY), rPredicate);
173 if (Reference<XInterface>(maNode, UNO_QUERY).is())
174 return true;
176 mxRoot = NULL;
177 return false;
183 bool PresenterConfigurationAccess::SetProperty (
184 const ::rtl::OUString& rsPropertyName,
185 const Any& rValue)
187 Reference<beans::XPropertySet> xProperties (maNode, UNO_QUERY);
188 if (xProperties.is())
190 xProperties->setPropertyValue(rsPropertyName, rValue);
191 return true;
193 else
194 return false;
200 Any PresenterConfigurationAccess::GetConfigurationNode (
201 const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
202 const OUString& sPathToNode)
204 if (sPathToNode.getLength() == 0)
205 return Any(rxNode);
209 if (rxNode.is())
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());
221 return Any();
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);
250 if (xAccess.is())
252 return xAccess->getByName(sKey);
254 else
256 return Any();
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());
279 if (xSetItem.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;
287 else
288 aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]);
291 else
292 bHasAllValues = false;
293 if (bHasAllValues)
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);
313 if (xSet.is())
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);
337 if (xSetItem.is())
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]),
362 UNO_QUERY);
363 if (xProperties.is())
364 if (rPredicate(aKeys[nItemIndex], xProperties))
365 return Any(xProperties);
368 return Any();
374 bool PresenterConfigurationAccess::IsStringPropertyEqual (
375 const ::rtl::OUString& rsValue,
376 const ::rtl::OUString& rsPropertyName,
377 const css::uno::Reference<css::beans::XPropertySet>& rxNode)
379 OUString sValue;
380 if (GetProperty(rxNode, rsPropertyName) >>= sValue)
381 return sValue == rsValue;
382 else
383 return false;
389 Any PresenterConfigurationAccess::GetProperty (
390 const Reference<beans::XPropertySet>& rxProperties,
391 const OUString& rsKey)
393 OSL_ASSERT(rxProperties.is());
394 if ( ! rxProperties.is())
395 return Any();
398 Reference<beans::XPropertySetInfo> xInfo (rxProperties->getPropertySetInfo());
399 if (xInfo.is())
400 if ( ! xInfo->hasPropertyByName(rsKey))
401 return Any();
402 return rxProperties->getPropertyValue(rsKey);
404 catch (beans::UnknownPropertyException&)
407 return Any();
413 } } // end of namespace sdext::tools