Bump version to 5.0-14
[LibreOffice.git] / sdext / source / presenter / PresenterConfigurationAccess.cxx
blob16914a9e917bdcb14d8882d0e70a7bba40d2568b
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>
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,
40 WriteMode eMode)
41 : mxRoot(),
42 maNode()
44 try
46 if (rxContext.is())
48 Sequence<Any> aCreationArguments(3);
49 aCreationArguments[0] = makeAny(beans::PropertyValue(
50 "nodepath",
52 makeAny(rsRootName),
53 beans::PropertyState_DIRECT_VALUE));
54 aCreationArguments[1] = makeAny(beans::PropertyValue(
55 "depth",
57 makeAny((sal_Int32)-1),
58 beans::PropertyState_DIRECT_VALUE));
59 aCreationArguments[2] = makeAny(beans::PropertyValue(
60 "lazywrite",
62 makeAny(true),
63 beans::PropertyState_DIRECT_VALUE));
65 OUString sAccessService;
66 if (eMode == READ_ONLY)
67 sAccessService = "com.sun.star.configuration.ConfigurationAccess";
68 else
69 sAccessService = "com.sun.star.configuration.ConfigurationUpdateAccess";
71 Reference<lang::XMultiServiceFactory> xProvider =
72 configuration::theDefaultProvider::get( rxContext );
73 mxRoot = xProvider->createInstanceWithArguments(
74 sAccessService, aCreationArguments);
75 maNode <<= mxRoot;
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
92 return mxRoot.is();
95 Any PresenterConfigurationAccess::GetConfigurationNode (const OUString& sPathToNode)
97 return GetConfigurationNode(
98 Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
99 sPathToNode);
102 bool PresenterConfigurationAccess::GoToChild (const OUString& rsPathToNode)
104 if ( ! IsValid())
105 return false;
107 Reference<container::XHierarchicalNameAccess> xNode (maNode, UNO_QUERY);
108 if (xNode.is())
110 maNode = GetConfigurationNode(
111 Reference<container::XHierarchicalNameAccess>(maNode, UNO_QUERY),
112 rsPathToNode);
113 if (Reference<XInterface>(maNode, UNO_QUERY).is())
114 return true;
117 mxRoot = NULL;
118 return false;
121 bool PresenterConfigurationAccess::GoToChild (const Predicate& rPredicate)
123 if ( ! IsValid())
124 return false;
126 maNode = Find(Reference<container::XNameAccess>(maNode,UNO_QUERY), rPredicate);
127 if (Reference<XInterface>(maNode, UNO_QUERY).is())
128 return true;
130 mxRoot = NULL;
131 return false;
134 bool PresenterConfigurationAccess::SetProperty (
135 const OUString& rsPropertyName,
136 const Any& rValue)
138 Reference<beans::XPropertySet> xProperties (maNode, UNO_QUERY);
139 if (xProperties.is())
141 xProperties->setPropertyValue(rsPropertyName, rValue);
142 return true;
144 else
145 return false;
148 Any PresenterConfigurationAccess::GetConfigurationNode (
149 const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
150 const OUString& sPathToNode)
152 if (sPathToNode.isEmpty())
153 return Any(rxNode);
157 if (rxNode.is())
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());
169 return Any();
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());
202 if (xSetItem.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;
210 else
211 aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]);
214 else
215 bHasAllValues = false;
216 if (bHasAllValues)
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);
233 if (xSet.is())
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]),
250 UNO_QUERY);
251 if (xProperties.is())
252 if (rPredicate(aKeys[nItemIndex], xProperties))
253 return Any(xProperties);
256 return Any();
259 bool PresenterConfigurationAccess::IsStringPropertyEqual (
260 const OUString& rsValue,
261 const OUString& rsPropertyName,
262 const css::uno::Reference<css::beans::XPropertySet>& rxNode)
264 OUString sValue;
265 if (GetProperty(rxNode, rsPropertyName) >>= sValue)
266 return sValue == rsValue;
267 else
268 return false;
271 Any PresenterConfigurationAccess::GetProperty (
272 const Reference<beans::XPropertySet>& rxProperties,
273 const OUString& rsKey)
275 OSL_ASSERT(rxProperties.is());
276 if ( ! rxProperties.is())
277 return Any();
280 Reference<beans::XPropertySetInfo> xInfo (rxProperties->getPropertySetInfo());
281 if (xInfo.is())
282 if ( ! xInfo->hasPropertyByName(rsKey))
283 return Any();
284 return rxProperties->getPropertyValue(rsKey);
286 catch (beans::UnknownPropertyException&)
289 return Any();
292 } } // end of namespace sdext::tools
294 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */