Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sd / source / console / PresenterConfigurationAccess.cxx
blob4e8494c1115c76f9e08cb45b99881f69453ee417
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/container/XHierarchicalNameAccess.hpp>
24 #include <com/sun/star/configuration/theDefaultProvider.hpp>
25 #include <com/sun/star/util/XChangesBatch.hpp>
26 #include <comphelper/propertysequence.hxx>
27 #include <osl/diagnose.h>
28 #include <comphelper/diagnose_ex.hxx>
29 #include <sal/log.hxx>
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::uno;
34 namespace sdext::presenter {
36 PresenterConfigurationAccess::PresenterConfigurationAccess (
37 const Reference<XComponentContext>& rxContext,
38 const OUString& rsRootName,
39 WriteMode eMode)
41 try
43 if (rxContext.is())
45 uno::Sequence<uno::Any> aCreationArguments(comphelper::InitAnyPropertySequence(
47 {"nodepath", uno::Any(rsRootName)},
48 {"depth", uno::Any(sal_Int32(-1))}
49 }));
51 OUString sAccessService;
52 if (eMode == READ_ONLY)
53 sAccessService = "com.sun.star.configuration.ConfigurationAccess";
54 else
55 sAccessService = "com.sun.star.configuration.ConfigurationUpdateAccess";
57 Reference<lang::XMultiServiceFactory> xProvider =
58 configuration::theDefaultProvider::get( rxContext );
59 mxRoot = xProvider->createInstanceWithArguments(
60 sAccessService, aCreationArguments);
61 maNode <<= mxRoot;
64 catch (const Exception&)
66 DBG_UNHANDLED_EXCEPTION("sdext.presenter", "caught exception while opening configuration");
70 PresenterConfigurationAccess::~PresenterConfigurationAccess()
74 bool PresenterConfigurationAccess::IsValid() const
76 return mxRoot.is();
79 Any PresenterConfigurationAccess::GetConfigurationNode (const OUString& sPathToNode)
81 return GetConfigurationNode(
82 Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
83 sPathToNode);
86 bool PresenterConfigurationAccess::GoToChild (const OUString& rsPathToNode)
88 if ( ! IsValid())
89 return false;
91 Reference<container::XHierarchicalNameAccess> xNode (maNode, UNO_QUERY);
92 if (xNode.is())
94 maNode = GetConfigurationNode(
95 Reference<container::XHierarchicalNameAccess>(maNode, UNO_QUERY),
96 rsPathToNode);
97 if (Reference<XInterface>(maNode, UNO_QUERY).is())
98 return true;
101 mxRoot = nullptr;
102 return false;
105 bool PresenterConfigurationAccess::GoToChild (const Predicate& rPredicate)
107 if ( ! IsValid())
108 return false;
110 maNode = Find(Reference<container::XNameAccess>(maNode,UNO_QUERY), rPredicate);
111 if (Reference<XInterface>(maNode, UNO_QUERY).is())
112 return true;
114 mxRoot = nullptr;
115 return false;
118 bool PresenterConfigurationAccess::SetProperty (
119 const OUString& rsPropertyName,
120 const Any& rValue)
122 Reference<beans::XPropertySet> xProperties (maNode, UNO_QUERY);
123 if (xProperties.is())
125 xProperties->setPropertyValue(rsPropertyName, rValue);
126 return true;
128 else
129 return false;
132 Any PresenterConfigurationAccess::GetConfigurationNode (
133 const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
134 const OUString& sPathToNode)
136 if (sPathToNode.isEmpty())
137 return Any(rxNode);
141 if (rxNode.is())
143 return rxNode->getByHierarchicalName(sPathToNode);
146 catch (const Exception&)
148 TOOLS_WARN_EXCEPTION("sdext.presenter", "caught exception while getting configuration node " << sPathToNode);
151 return Any();
154 Reference<beans::XPropertySet> PresenterConfigurationAccess::GetNodeProperties (
155 const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
156 const OUString& rsPathToNode)
158 return Reference<beans::XPropertySet>(GetConfigurationNode(rxNode, rsPathToNode), UNO_QUERY);
161 void PresenterConfigurationAccess::CommitChanges()
163 Reference<util::XChangesBatch> xConfiguration (mxRoot, UNO_QUERY);
164 if (xConfiguration.is())
165 xConfiguration->commitChanges();
168 void PresenterConfigurationAccess::ForAll (
169 const Reference<container::XNameAccess>& rxContainer,
170 const ::std::vector<OUString>& rArguments,
171 const ItemProcessor& rProcessor)
173 if (!rxContainer.is())
174 return;
176 ::std::vector<Any> aValues(rArguments.size());
177 const Sequence<OUString> aKeys (rxContainer->getElementNames());
178 for (const OUString& rsKey : aKeys)
180 bool bHasAllValues (true);
181 Reference<container::XNameAccess> xSetItem (rxContainer->getByName(rsKey), UNO_QUERY);
182 Reference<beans::XPropertySet> xSet (xSetItem, UNO_QUERY);
183 OSL_ASSERT(xSet.is());
184 if (xSetItem.is())
186 // Get from the current item of the container the children
187 // that match the names in the rArguments list.
188 for (size_t nValueIndex=0; nValueIndex<aValues.size(); ++nValueIndex)
190 if ( ! xSetItem->hasByName(rArguments[nValueIndex]))
191 bHasAllValues = false;
192 else
193 aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]);
196 else
197 bHasAllValues = false;
198 if (bHasAllValues)
199 rProcessor(aValues);
203 void PresenterConfigurationAccess::ForAll (
204 const Reference<container::XNameAccess>& rxContainer,
205 const PropertySetProcessor& rProcessor)
207 if (rxContainer.is())
209 const Sequence<OUString> aKeys (rxContainer->getElementNames());
210 for (const OUString& rsKey : aKeys)
212 Reference<beans::XPropertySet> xSet (rxContainer->getByName(rsKey), UNO_QUERY);
213 if (xSet.is())
214 rProcessor(rsKey, xSet);
219 Any PresenterConfigurationAccess::Find (
220 const Reference<container::XNameAccess>& rxContainer,
221 const Predicate& rPredicate)
223 if (rxContainer.is())
225 const Sequence<OUString> aKeys (rxContainer->getElementNames());
226 for (const auto& rKey : aKeys)
228 Reference<beans::XPropertySet> xProperties (
229 rxContainer->getByName(rKey),
230 UNO_QUERY);
231 if (xProperties.is())
232 if (rPredicate(rKey, xProperties))
233 return Any(xProperties);
236 return Any();
239 bool PresenterConfigurationAccess::IsStringPropertyEqual (
240 std::u16string_view rsValue,
241 const OUString& rsPropertyName,
242 const css::uno::Reference<css::beans::XPropertySet>& rxNode)
244 OUString sValue;
245 if (GetProperty(rxNode, rsPropertyName) >>= sValue)
246 return sValue == rsValue;
247 else
248 return false;
251 Any PresenterConfigurationAccess::GetProperty (
252 const Reference<beans::XPropertySet>& rxProperties,
253 const OUString& rsKey)
255 OSL_ASSERT(rxProperties.is());
256 if ( ! rxProperties.is())
257 return Any();
260 Reference<beans::XPropertySetInfo> xInfo (rxProperties->getPropertySetInfo());
261 if (xInfo.is())
262 if ( ! xInfo->hasPropertyByName(rsKey))
263 return Any();
264 return rxProperties->getPropertyValue(rsKey);
266 catch (beans::UnknownPropertyException&)
269 return Any();
272 } // end of namespace sdext::presenter
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */