Updated core
[LibreOffice.git] / sdext / source / presenter / PresenterConfigurationAccess.cxx
blob3a36cb332623f14a0e33cb33e72122b3575b017b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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,
39 WriteMode eMode)
40 : mxRoot(),
41 maNode()
43 try
45 if (rxContext.is())
47 Sequence<Any> aCreationArguments(3);
48 aCreationArguments[0] = makeAny(beans::PropertyValue(
49 "nodepath",
51 makeAny(rsRootName),
52 beans::PropertyState_DIRECT_VALUE));
53 aCreationArguments[1] = makeAny(beans::PropertyValue(
54 "depth",
56 makeAny((sal_Int32)-1),
57 beans::PropertyState_DIRECT_VALUE));
58 aCreationArguments[2] = makeAny(beans::PropertyValue(
59 "lazywrite",
61 makeAny(true),
62 beans::PropertyState_DIRECT_VALUE));
64 OUString sAccessService;
65 if (eMode == READ_ONLY)
66 sAccessService = "com.sun.star.configuration.ConfigurationAccess";
67 else
68 sAccessService = "com.sun.star.configuration.ConfigurationUpdateAccess";
70 Reference<lang::XMultiServiceFactory> xProvider =
71 configuration::theDefaultProvider::get( rxContext );
72 mxRoot = xProvider->createInstanceWithArguments(
73 sAccessService, aCreationArguments);
74 maNode <<= mxRoot;
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
91 return mxRoot.is();
94 Any PresenterConfigurationAccess::GetConfigurationNode (const OUString& sPathToNode)
96 return GetConfigurationNode(
97 Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
98 sPathToNode);
101 bool PresenterConfigurationAccess::GoToChild (const OUString& rsPathToNode)
103 if ( ! IsValid())
104 return false;
106 Reference<container::XHierarchicalNameAccess> xNode (maNode, UNO_QUERY);
107 if (xNode.is())
109 maNode = GetConfigurationNode(
110 Reference<container::XHierarchicalNameAccess>(maNode, UNO_QUERY),
111 rsPathToNode);
112 if (Reference<XInterface>(maNode, UNO_QUERY).is())
113 return true;
116 mxRoot = NULL;
117 return false;
120 bool PresenterConfigurationAccess::GoToChild (const Predicate& rPredicate)
122 if ( ! IsValid())
123 return false;
125 maNode = Find(Reference<container::XNameAccess>(maNode,UNO_QUERY), rPredicate);
126 if (Reference<XInterface>(maNode, UNO_QUERY).is())
127 return true;
129 mxRoot = NULL;
130 return false;
133 bool PresenterConfigurationAccess::SetProperty (
134 const OUString& rsPropertyName,
135 const Any& rValue)
137 Reference<beans::XPropertySet> xProperties (maNode, UNO_QUERY);
138 if (xProperties.is())
140 xProperties->setPropertyValue(rsPropertyName, rValue);
141 return true;
143 else
144 return false;
147 Any PresenterConfigurationAccess::GetConfigurationNode (
148 const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
149 const OUString& sPathToNode)
151 if (sPathToNode.isEmpty())
152 return Any(rxNode);
156 if (rxNode.is())
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());
168 return Any();
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());
201 if (xSetItem.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;
209 else
210 aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]);
213 else
214 bHasAllValues = false;
215 if (bHasAllValues)
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);
232 if (xSet.is())
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]),
249 UNO_QUERY);
250 if (xProperties.is())
251 if (rPredicate(aKeys[nItemIndex], xProperties))
252 return Any(xProperties);
255 return Any();
258 bool PresenterConfigurationAccess::IsStringPropertyEqual (
259 const OUString& rsValue,
260 const OUString& rsPropertyName,
261 const css::uno::Reference<css::beans::XPropertySet>& rxNode)
263 OUString sValue;
264 if (GetProperty(rxNode, rsPropertyName) >>= sValue)
265 return sValue == rsValue;
266 else
267 return false;
270 Any PresenterConfigurationAccess::GetProperty (
271 const Reference<beans::XPropertySet>& rxProperties,
272 const OUString& rsKey)
274 OSL_ASSERT(rxProperties.is());
275 if ( ! rxProperties.is())
276 return Any();
279 Reference<beans::XPropertySetInfo> xInfo (rxProperties->getPropertySetInfo());
280 if (xInfo.is())
281 if ( ! xInfo->hasPropertyByName(rsKey))
282 return Any();
283 return rxProperties->getPropertyValue(rsKey);
285 catch (beans::UnknownPropertyException&)
288 return Any();
291 } } // end of namespace sdext::tools
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */