Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sdext / source / presenter / PresenterConfigurationAccess.cxx
blobc1ca22f77dd039c9ca3cb4a603f18ceef98a475f
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 <comphelper/propertysequence.hxx>
28 #include <osl/diagnose.h>
29 #include <tools/diagnose_ex.h>
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::uno;
34 namespace sdext { namespace presenter {
36 const OUString PresenterConfigurationAccess::msPresenterScreenRootName =
37 "/org.openoffice.Office.PresenterScreen/";
39 PresenterConfigurationAccess::PresenterConfigurationAccess (
40 const Reference<XComponentContext>& rxContext,
41 const OUString& rsRootName,
42 WriteMode eMode)
43 : mxRoot(),
44 maNode()
46 try
48 if (rxContext.is())
50 uno::Sequence<uno::Any> aCreationArguments(comphelper::InitAnyPropertySequence(
52 {"nodepath", uno::Any(rsRootName)},
53 {"depth", uno::Any(sal_Int32(-1))}
54 }));
56 OUString sAccessService;
57 if (eMode == READ_ONLY)
58 sAccessService = "com.sun.star.configuration.ConfigurationAccess";
59 else
60 sAccessService = "com.sun.star.configuration.ConfigurationUpdateAccess";
62 Reference<lang::XMultiServiceFactory> xProvider =
63 configuration::theDefaultProvider::get( rxContext );
64 mxRoot = xProvider->createInstanceWithArguments(
65 sAccessService, aCreationArguments);
66 maNode <<= mxRoot;
69 catch (const Exception&)
71 DBG_UNHANDLED_EXCEPTION("sdext.presenter", "caught exception while opening configuration");
75 PresenterConfigurationAccess::~PresenterConfigurationAccess()
79 bool PresenterConfigurationAccess::IsValid() const
81 return mxRoot.is();
84 Any PresenterConfigurationAccess::GetConfigurationNode (const OUString& sPathToNode)
86 return GetConfigurationNode(
87 Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
88 sPathToNode);
91 bool PresenterConfigurationAccess::GoToChild (const OUString& rsPathToNode)
93 if ( ! IsValid())
94 return false;
96 Reference<container::XHierarchicalNameAccess> xNode (maNode, UNO_QUERY);
97 if (xNode.is())
99 maNode = GetConfigurationNode(
100 Reference<container::XHierarchicalNameAccess>(maNode, UNO_QUERY),
101 rsPathToNode);
102 if (Reference<XInterface>(maNode, UNO_QUERY).is())
103 return true;
106 mxRoot = nullptr;
107 return false;
110 bool PresenterConfigurationAccess::GoToChild (const Predicate& rPredicate)
112 if ( ! IsValid())
113 return false;
115 maNode = Find(Reference<container::XNameAccess>(maNode,UNO_QUERY), rPredicate);
116 if (Reference<XInterface>(maNode, UNO_QUERY).is())
117 return true;
119 mxRoot = nullptr;
120 return false;
123 bool PresenterConfigurationAccess::SetProperty (
124 const OUString& rsPropertyName,
125 const Any& rValue)
127 Reference<beans::XPropertySet> xProperties (maNode, UNO_QUERY);
128 if (xProperties.is())
130 xProperties->setPropertyValue(rsPropertyName, rValue);
131 return true;
133 else
134 return false;
137 Any PresenterConfigurationAccess::GetConfigurationNode (
138 const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
139 const OUString& sPathToNode)
141 if (sPathToNode.isEmpty())
142 return Any(rxNode);
146 if (rxNode.is())
148 return rxNode->getByHierarchicalName(sPathToNode);
151 catch (const Exception& rException)
153 SAL_WARN("sdext.presenter", "caught exception while getting configuration node " << sPathToNode << " : " << rException);
156 return Any();
159 Reference<beans::XPropertySet> PresenterConfigurationAccess::GetNodeProperties (
160 const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
161 const OUString& rsPathToNode)
163 return Reference<beans::XPropertySet>(GetConfigurationNode(rxNode, rsPathToNode), UNO_QUERY);
166 void PresenterConfigurationAccess::CommitChanges()
168 Reference<util::XChangesBatch> xConfiguration (mxRoot, UNO_QUERY);
169 if (xConfiguration.is())
170 xConfiguration->commitChanges();
173 void PresenterConfigurationAccess::ForAll (
174 const Reference<container::XNameAccess>& rxContainer,
175 const ::std::vector<OUString>& rArguments,
176 const ItemProcessor& rProcessor)
178 if (rxContainer.is())
180 ::std::vector<Any> aValues(rArguments.size());
181 Sequence<OUString> aKeys (rxContainer->getElementNames());
182 for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
184 bool bHasAllValues (true);
185 const OUString& rsKey (aKeys[nItemIndex]);
186 Reference<container::XNameAccess> xSetItem (rxContainer->getByName(rsKey), UNO_QUERY);
187 Reference<beans::XPropertySet> xSet (xSetItem, UNO_QUERY);
188 OSL_ASSERT(xSet.is());
189 if (xSetItem.is())
191 // Get from the current item of the container the children
192 // that match the names in the rArguments list.
193 for (size_t nValueIndex=0; nValueIndex<aValues.size(); ++nValueIndex)
195 if ( ! xSetItem->hasByName(rArguments[nValueIndex]))
196 bHasAllValues = false;
197 else
198 aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]);
201 else
202 bHasAllValues = false;
203 if (bHasAllValues)
204 rProcessor(aValues);
209 void PresenterConfigurationAccess::ForAll (
210 const Reference<container::XNameAccess>& rxContainer,
211 const PropertySetProcessor& rProcessor)
213 if (rxContainer.is())
215 Sequence<OUString> aKeys (rxContainer->getElementNames());
216 for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
218 const OUString& rsKey (aKeys[nItemIndex]);
219 Reference<beans::XPropertySet> xSet (rxContainer->getByName(rsKey), UNO_QUERY);
220 if (xSet.is())
221 rProcessor(rsKey, xSet);
226 Any PresenterConfigurationAccess::Find (
227 const Reference<container::XNameAccess>& rxContainer,
228 const Predicate& rPredicate)
230 if (rxContainer.is())
232 Sequence<OUString> aKeys (rxContainer->getElementNames());
233 for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
235 Reference<beans::XPropertySet> xProperties (
236 rxContainer->getByName(aKeys[nItemIndex]),
237 UNO_QUERY);
238 if (xProperties.is())
239 if (rPredicate(aKeys[nItemIndex], xProperties))
240 return Any(xProperties);
243 return Any();
246 bool PresenterConfigurationAccess::IsStringPropertyEqual (
247 const OUString& rsValue,
248 const OUString& rsPropertyName,
249 const css::uno::Reference<css::beans::XPropertySet>& rxNode)
251 OUString sValue;
252 if (GetProperty(rxNode, rsPropertyName) >>= sValue)
253 return sValue == rsValue;
254 else
255 return false;
258 Any PresenterConfigurationAccess::GetProperty (
259 const Reference<beans::XPropertySet>& rxProperties,
260 const OUString& rsKey)
262 OSL_ASSERT(rxProperties.is());
263 if ( ! rxProperties.is())
264 return Any();
267 Reference<beans::XPropertySetInfo> xInfo (rxProperties->getPropertySetInfo());
268 if (xInfo.is())
269 if ( ! xInfo->hasPropertyByName(rsKey))
270 return Any();
271 return rxProperties->getPropertyValue(rsKey);
273 catch (beans::UnknownPropertyException&)
276 return Any();
279 } } // end of namespace sdext::tools
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */