Bump version to 21.06.18.1
[LibreOffice.git] / sd / source / ui / tools / ConfigurationAccess.cxx
blob9b14a03a1768f7481123cadc7bb085033d431960
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 <tools/ConfigurationAccess.hxx>
21 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
22 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
23 #include <com/sun/star/configuration/theDefaultProvider.hpp>
24 #include <com/sun/star/container/XNameAccess.hpp>
25 #include <com/sun/star/util/XChangesBatch.hpp>
26 #include <comphelper/processfactory.hxx>
27 #include <comphelper/propertysequence.hxx>
28 #include <tools/diagnose_ex.h>
29 #include <sal/log.hxx>
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::uno;
34 namespace sd::tools {
36 ConfigurationAccess::ConfigurationAccess (
37 const Reference<XComponentContext>& rxContext,
38 const OUString& rsRootName,
39 const WriteMode eMode)
40 : mxRoot()
42 Reference<lang::XMultiServiceFactory> xProvider =
43 configuration::theDefaultProvider::get( rxContext );
44 Initialize(xProvider, rsRootName, eMode);
47 ConfigurationAccess::ConfigurationAccess (
48 const OUString& rsRootName,
49 const WriteMode eMode)
50 : mxRoot()
52 Reference<lang::XMultiServiceFactory> xProvider =
53 configuration::theDefaultProvider::get( ::comphelper::getProcessComponentContext() );
54 Initialize(xProvider, rsRootName, eMode);
57 void ConfigurationAccess::Initialize (
58 const Reference<lang::XMultiServiceFactory>& rxProvider,
59 const OUString& rsRootName,
60 const WriteMode eMode)
62 try
64 Sequence<Any> aCreationArguments(comphelper::InitAnyPropertySequence(
66 {"nodepath", makeAny(rsRootName)},
67 {"depth", makeAny(sal_Int32(-1))}
68 }));
70 OUString sAccessService;
71 if (eMode == READ_ONLY)
72 sAccessService = "com.sun.star.configuration.ConfigurationAccess";
73 else
74 sAccessService = "com.sun.star.configuration.ConfigurationUpdateAccess";
76 mxRoot = rxProvider->createInstanceWithArguments(
77 sAccessService,
78 aCreationArguments);
80 catch (Exception&)
82 DBG_UNHANDLED_EXCEPTION("sd.tools");
86 Any ConfigurationAccess::GetConfigurationNode (
87 const OUString& sPathToNode)
89 return GetConfigurationNode(
90 Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
91 sPathToNode);
94 Any ConfigurationAccess::GetConfigurationNode (
95 const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
96 const OUString& sPathToNode)
98 if (sPathToNode.isEmpty())
99 return Any(rxNode);
103 if (rxNode.is())
105 return rxNode->getByHierarchicalName(sPathToNode);
108 catch (const Exception&)
110 TOOLS_WARN_EXCEPTION("sd", "caught exception while getting configuration node" << sPathToNode);
113 return Any();
116 void ConfigurationAccess::CommitChanges()
118 Reference<util::XChangesBatch> xConfiguration (mxRoot, UNO_QUERY);
119 if (xConfiguration.is())
120 xConfiguration->commitChanges();
123 void ConfigurationAccess::ForAll (
124 const Reference<container::XNameAccess>& rxContainer,
125 const ::std::vector<OUString>& rArguments,
126 const Functor& rFunctor)
128 if (!rxContainer.is())
129 return;
131 ::std::vector<Any> aValues(rArguments.size());
132 const Sequence<OUString> aKeys (rxContainer->getElementNames());
133 for (const OUString& rsKey : aKeys)
135 Reference<container::XNameAccess> xSetItem (rxContainer->getByName(rsKey), UNO_QUERY);
136 if (xSetItem.is())
138 // Get from the current item of the container the children
139 // that match the names in the rArguments list.
140 for (size_t nValueIndex=0; nValueIndex<aValues.size(); ++nValueIndex)
141 aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]);
143 rFunctor(rsKey, aValues);
147 void ConfigurationAccess::FillList(
148 const Reference<container::XNameAccess>& rxContainer,
149 const OUString& rsArgument,
150 ::std::vector<OUString>& rList)
154 if (rxContainer.is())
156 Sequence<OUString> aKeys (rxContainer->getElementNames());
157 rList.resize(aKeys.getLength());
158 for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
160 Reference<container::XNameAccess> xSetItem (
161 rxContainer->getByName(aKeys[nItemIndex]), UNO_QUERY);
162 if (xSetItem.is())
164 xSetItem->getByName(rsArgument) >>= rList[nItemIndex];
169 catch (RuntimeException&)
173 } // end of namespace sd::tools
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */